2013-10-08 46 views
2

我有這樣的JSON結構:在我的C#示例解釋JSON結構

 { 
     "Root": { 
      "data": [ 
       { 
        "CardName": "card1", 
        "functions": [ 
         { 
          "State": "OPEN", 
       "State": "INHERENT" 
         } 
        ] 
       }, 
       { 
        "CardName": "card2", 
        "functions": [ 
         { 
          "State": "CLOSED", 
       "State": "INHERENT" 
         } 
        ] 
       } 
      ] 
     } 
    } 

而且我的C#類:

[DataContract] 
public class Card 
{ 
    [DataMember(Name = "CardName")] 
    public string CardName { get; set; } 

    [DataMember(Name = "functions")] 
    public List<Function> Functions { get; set; } 
} 

[DataContract] 
public class Function 
{ 

    [DataMember(Name = "State")] 
    public string State { get; set; } 
} 

我想解析結構,以獲得一張卡片列表,每張卡片包含一系列功能。

這時我試圖這樣的:

 string content = string.Empty; 
     using (StreamReader sr = new StreamReader("json")) 
     { 
      string line; 
      while ((line = sr.ReadLine()) != null) 
      { 
       content += line; 
      } 
     } 

    List<Card> dynObj = JsonConvert.DeserializeObject<Card>(content); 

,但我只得到空的列表。你能告訴我問題在哪裏嗎?

+2

你也可以嘗試其他方法:創建一個Card,將它序列化爲JSON並查看輸出結果是什麼? – Rup

+0

你爲什麼使用動態? - 結果是已知類型(文章),因此您可以編寫文章或變種。 –

+0

您的文章類的定義在上面丟失。 –

回答

8

通過在Visual Studio粘貼JSON(編輯>選擇性粘貼 - >粘貼JSON作爲類),它告訴我的數據類應該看起來像這樣。

public class Rootobject 
{ 
    public Root Root { get; set; } 
} 

public class Root 
{ 
    public Datum[] data { get; set; } 
} 

public class Datum 
{ 
    public string CardName { get; set; } 
    public Function[] functions { get; set; } 
} 

public class Function 
{ 
    public string State { get; set; } 
} 
+2

+1! – JBeagle

2

http://json2csharp.com/

public class Function 
{ 
    public string State { get; set; } 
} 

public class Datum 
{ 
    public string CardName { get; set; } 
    public List<Function> functions { get; set; } 
} 

public class Root 
{ 
    public List<Datum> data { get; set; } 
} 

public class RootObject 
{ 
    public Root Root { get; set; } 
} 
1

請如下改變你的jsona並嘗試

"Root": { 
    "data": [{ 
     "CardName": "card1", 
     "functions": [{ 
      "State": "OPEN" 
     }, { 
      "State": "INHERENT" 
     }] 
    }, { 
     "CardName": "card2", 
     "functions": [{ 
      "State": "CLOSED" 
     }, { 
      "State": "INHERENT" 
     }] 
    }] 
} 
1

假設丟失類是:

public class Article 
{ 
    public List<Card> Root { get; set; } 
} 

的JSON中有一個錯誤,它應該是:

{ 
    "Root": [ 
      { 
       "CardName": "card1", 
       "functions": [ 
        { 
         "State": "OPEN", 
        }, 

        { 
         "State": "INHERENT" 
        } 
       ] 
      }, 
      { 
       "CardName": "card2", 
       "functions": [ 
        { 
         "State": "CLOSED" 
        }, 
        { 
         "State": "INHERENT" 
        } 
       ] 
      } 
     ] 

注意,功能應各自在{}而根類應該包含一張卡片列表。

或者,您可以反序列化爲List,然後跳過包裝「Root」。

1

代碼有很多問題。我已經修復了以下代碼:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Objects; 
using System.Data.SqlClient; 
using System.Globalization; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.Text; 
using System.Runtime.Serialization.Json; 
using System.Threading; 
using System.Xml; 
using ConsoleDemo.Controller; 
using ConsoleDemo.Model; 
using Microsoft.Practices.Unity; 


namespace ConsoleDemo 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var data = @"{""Root"": {""data"": [{""CardName"": ""card1"",""functions"": [{""State"": ""OPEN""},{""State"": ""INHERENT""}]},{""CardName"": ""card2"",""functions"": [{""State"": ""CLOSED""},{""State"": ""INHERENT""}]}]}"; 
      RootClass dynObj = JsonHelper.JsonDeserialize<RootClass>(data); //Get the object 
      Console.ReadKey(); 
     } 
    } 

    [DataContract] 
    public class RootClass 
    { 
     [DataMember(Name = "Root")] 
     public Data Root { get; set; } 
    } 

    [DataContract] 
    public class Data 
    { 
     [DataMember(Name = "data")] 
     public List<Card> data { get; set; } 
    } 

    [DataContract] 
    public class Card 
    { 
     [DataMember(Name = "CardName")] 
     public string CardName { get; set; } 

     [DataMember(Name = "functions")] 
     public List<Function> Functions { get; set; } 
    } 

    [DataContract] 
    public class Function 
    { 

     [DataMember(Name = "State")] 
     public string State { get; set; } 
    } 

    public class JsonHelper 
    { 
     /// <summary> 
     /// JSON Serialization 
     /// </summary> 
     public static string JsonSerializer<T>(T t) 
     { 
      var ser = new DataContractJsonSerializer(typeof(T)); 
      var ms = new MemoryStream(); 
      ser.WriteObject(ms, t); 
      var jsonString = Encoding.UTF8.GetString(ms.ToArray()); 
      ms.Close(); 
      return jsonString; 
     } 
     /// <summary> 
     /// JSON Deserialization 
     /// </summary> 
     public static T JsonDeserialize<T>(string jsonString) 
     { 
      var ser = new DataContractJsonSerializer(typeof(T)); 
      var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
      var obj = (T)ser.ReadObject(ms); 
      return obj; 
     } 
    } 

}