2016-07-13 92 views
1

我試圖解析一個相當複雜/不必要的複雜JSON輸出在C#中使用newtonsoft,但由於某種原因,我的解析器總是返回null。我搜索了所有的SO,似乎找不到解決方案。C#解析JSON與多個對象和數組newtonsoft

一個JSON文件我試圖解析的例子:

{ 
    "success": 1, 
    "d": { 
    "gameData": { 
     "MJ2Y7tDg": { 
     "scores": [ 
      { 
      "max": 1.83, 
      "avg": 1.73, 
      "rest": 2, 
      "active": true, 
      "scoreid": "2c556xv464x0x4vtqc" 
      }, 
      { 
      "max": 3.47, 
      "avg": 3.24, 
      "rest": 2, 
      "active": true, 
      "scoreid": "2c556xv498x0x0" 
      }, 
      { 
      "max": 6.06, 
      "avg": 5.08, 
      "rest": 1, 
      "active": true, 
      "scoreid": "2c556xv464x0x4vtqd" 
      } 
     ], 
     "count": 62, 
     "highlight": [ 
      false, 
      true 
     ] 
     }, 
     "jZICYUQu": { 
     "scores": [ 
      { 
      "max": 2.25, 
      "avg": 2.13, 
      "rest": null, 
      "active": true, 
      "scoreid": "2c5guxv464x0x4vuiv" 
      }, 
      { 
      "max": 3.55, 
      "avg": 3.29, 
      "rest": null, 
      "active": true, 
      "scoreid": "2c5guxv498x0x0" 
      }, 
      { 
      "max": 3.9, 
      "avg": 3.33, 
      "rest": null, 
      "active": true, 
      "scoreid": "2c5guxv464x0x4vuj0" 
      } 
     ], 
     "count": 62, 
     "highlight": [ 
      false, 
      false 
     ] 
     } 
    } 
    } 
} 

這是我到目前爲止,我是很新的JSON扯皮:)

public class RootObject 
    { 
     public int success { get; set; } 
     public List<d> d { get; set; } 
    } 

    public class d 
    { 
     public List<gameData> gameData { get; set; } 
    } 

    public class gameData 
    { 
     public IDictionary<string, Score> gameData{ get; set; } 
     public List<scores[]> GameList; 
    } 
    public class Score 
    { 
     public double max { get; set; } 
     public double avg { get; set; } 
     public int rest { get; set; } 
     public bool active { get; set; } 
     public string scoreid { get; set; } 
    } 

與任何人更多的JSON爭論經驗知道如何得到這個工作?

Thankyou in advanced。因爲你的類的結構不正確類似於JSON的結構 P.S目前我在高中時,學習C#

+0

請出示你的代碼 –

+0

他表明他試圖反序列化到類的代碼,還有什麼必要? –

回答

2

解析器返回null。類正確的結構將是:

public class RootObject 
{ 
    public int success { get; set; } 
    public Class_d d { get; set; } 
} 

public class Class_d 
{ 
    public Dictionary<string, GameData> gameData { get; set; } 
} 

public class GameData 
{ 
    public List<Score> scores { get; set; } 
    public int count { get; set; } 
    public bool[] highlight { get; set; } 
} 

public class Score 
{ 
    public decimal max { get; set; } 
    public decimal avg { get; set; } 
    public int? rest { get; set; } 
    public bool active { get; set; } 
    public string scoreid { get; set; } 
} 

,您可以按如下方式使用它:

string json = "..."; // the JSON in your example 
RootObject root = JsonConvert.DeserializeObject<RootObject>(json); 
+0

謝謝Felix-b。還有一件事,我將如何使用console.writeline轉儲數據。 –