2016-12-06 4 views
2

ファイルからJsonを読み込み、Newtonsoft Jsonを使用して逆シリアル化しようとするとnullが返されます。私はhttp://json2csharp.com/ からJSONクラスを構築しています。私はStreamReaderを使用するときに\ n、\ rなどの特殊記号が導入されているため、nullがそれを示す理由がわからない。助けてください。ファイルからjsonを読み込み、デシリアライズすると常にnullが返される

JSON(私はJsonLintとその有効なJSONで確認)

{ 
    "Machine Learning Functions": [{ 
     "Function": "JaccardDistance", 
     "ArgCount": 2, 
     "Arg1": "Point1", 
     "Arg1Type": "Point", 
     "Arg2": "Point2", 
     "Arg2Type": "Point", 
     "Return": "distance", 
     "ReturnType": "Double" 
    }], 
    "Math Functions": [{ 
     "Function": "Cosine", 
     "ArgCount": 2, 
     "Arg1": "document1", 
     "Arg1Type": "String", 
     "Arg2": "document2", 
     "Arg2Type": "String", 
     "Return": "angle", 
     "ReturnType": "Integer" 
    }, { 
     "Function": "SQRT", 
     "ArgCount": 1, 
     "Arg1": "SomeNumber", 
     "Arg1Type": "Integer", 
     "Return": "Number", 
     "ReturnType": "Integer" 
    }] 

} 

(json2csharpから取られた)C#コード

public class MachineLearningFunction 
    { 
     public string Function { get; set; } 
     public int ArgCount { get; set; } 
     public string Arg1 { get; set; } 
     public string Arg1Type { get; set; } 
     public string Arg2 { get; set; } 
     public string Arg2Type { get; set; } 
     public string Return { get; set; } 
     public string ReturnType { get; set; } 
    } 

    public class MathFunction 
    { 
     public string Function { get; set; } 
     public int ArgCount { get; set; } 
     public string Arg1 { get; set; } 
     public string Arg1Type { get; set; } 
     public string Arg2 { get; set; } 
     public string Arg2Type { get; set; } 
     public string Return { get; set; } 
     public string ReturnType { get; set; } 
    } 

    public class RootObject 
    { 
     public List<MachineLearningFunction> MachineLearningFunctions { get; set; } 
     public List<MathFunction> MathFunctions { get; set; } 
    } 

たときに、次のように、このJSONをファイルに保存されていると私は読んでいます私は\ n、\ rなどのようないくつかの特別なcharectersを導入して文字列を読んでブレークポイントを保持します。しかし、私がデシリアライズしようとすると、ブレークポイントはnullを表示し、リストを反復するときにnull参照例外が発生します。

string json = string.Empty; 
      using (StreamReader reader = new StreamReader(@"C:\Users\Nikh\OneDrive\Documents\Application/json.txt")) 
      { 
       json = reader.ReadToEnd(); 
      } 
ParseAndConstructJson(json); 

public void ParseAndConstructJson(string json) //Using Newtonsoft json 
     { 
      RootObject obj = JsonConvert.DeserializeObject<RootObject>(json); 
      foreach (var item in obj.MachineLearningFunctions) 
      { 
       MessageBox.Show(item.Function); 
      }//DataGrid dg = new DataGrid(); 
} 

答えて

3

あなたはJSONファイルにマップするRootObjectJsonPropertyあなたの特性を定義する必要があります。

public class RootObject 
{ 
    [JsonProperty(PropertyName ="Machine Learning Functions")] 
    public List<MachineLearningFunction> MachineLearningFunctions { get; set; } 

    [JsonProperty(PropertyName ="Math Functions")] 
    public List<MathFunction> MathFunctions { get; set; } 
} 
+0

ありがとうございます。 – nikhil

+0

@nikhilあなたが助けてくれたら、答えを正しいものとしてマークすることができます。 – mybirthname

0

からスペースを削除:JSONファイルに 「機械学習機能」と「数学関数」と、それは何の問題もなく、あなたのオブジェクトをデシリアライズします。

関連する問題