2015-04-17 66 views
2

我是新來JSON.NET,我需要幫助反序列化JSON如下反序列化JSON陣列與不同類型的

{ 
    "items": [ 
     [10, "file1", "command 1"], 
     [20, "file2", "command 2"], 
     [30, "file3", "command 3"] 
    ] 
} 

這個

IList<Item> Items {get; set;} 

class Item 
{ 
    public int Id  {get; set} 
    public string File {get; set} 
    public string Command {get; set} 
} 

在JSON的內容始終是以相同的順序。

+0

你有什麼問題與JSON.NET? –

+0

但是,如果我嘗試反序列化到列表,JSON.NET抱怨說它不能填充數組 – magol

+0

好的讓我試試這個代碼,請稍候。 –

回答

4

您可以使用自定義JsonConverter每個孩子數組轉換的JSON到Item。下面是你需要的變流器代碼:

class ItemConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return (objectType == typeof(Item)); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     JArray array = JArray.Load(reader); 
     return new Item 
     { 
      Id = (int)array[0], 
      File = (string)array[1], 
      Command = (string)array[2] 
     }; 
    } 

    public override bool CanWrite 
    { 
     get { return false; } 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 
} 

隨着上述轉換器中,你可以反序列化到你的類容易,如下面所示:

class Program 
{ 
    static void Main(string[] args) 
    { 
     string json = @" 
     { 
      ""items"": [ 
       [10, ""file1"", ""command 1""], 
       [20, ""file2"", ""command 2""], 
       [30, ""file3"", ""command 3""] 
      ] 
     }"; 

     Foo foo = JsonConvert.DeserializeObject<Foo>(json, new ItemConverter()); 

     foreach (Item item in foo.Items) 
     { 
      Console.WriteLine("Id: " + item.Id); 
      Console.WriteLine("File: " + item.File); 
      Console.WriteLine("Command: " + item.Command); 
      Console.WriteLine(); 
     } 
    } 
} 

class Foo 
{ 
    public List<Item> Items { get; set; } 
} 

class Item 
{ 
    public int Id { get; set; } 
    public string File { get; set; } 
    public string Command { get; set; } 
} 

輸出:

Id: 10 
File: file1 
Command: command 1 

Id: 20 
File: file2 
Command: command 2 

Id: 30 
File: file3 
Command: command 3 

小提琴:https://dotnetfiddle.net/RXggvl

0

反序列化的JSON應該是這樣的

{ 
    "items": [ 
     {Id: 10, File: "file1", Command: "command 1"}, 
     {Id: 20, File: "file2", Command: "command 2"}, 
     {Id: 30, File: "file3", Command: "command 3"} 
    ] 
} 

這將反序列化

期間,分別對應variables Id, File and Command to properties Id, File and Command您可以用下面的代碼

public List<Item> DeserializeJSON(string jsonString) 
{ 
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Item>)); 
    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
    var obj = (List<Item>)ser.ReadObject(stream); 
    return obj; 
} 
反序列化
+0

是的,如果JSON以這種方式形成,那就沒有問題了。但現在不是。是否有任何方法讓JSON.NET將數組中的數據轉換爲屬性? – magol

+0

@magol,因爲這是一個字符串,你的類並不複雜,所以可以在不使用序列化器的情況下手動解析字符串並將其存儲在列表集合中。但是這種類型的解決方案不是通用的,它只針對這個json字符串 – Kira

0

你可以這樣做通過使用中間類型來捕獲從Json和地圖到戰後類的轉換DS。所以首先我們有中級班:

public class IntermediateType 
{ 
    public object[][] items { get; set; } 
} 

現在我們可以給你結果是這樣的:

var json = "{\"items\": [ [10, \"file1\", \"command 1\"], [20, \"file2\", \"command 2\"], [30, \"file3\", \"command 3\"] ]}"; 

var result = JsonConvert 
    .DeserializeObject<IntermediateType>(json) 
    .items 
    .Select(o => new Item 
    { 
     Id = int.Parse(o[0].ToString()), 
     File = (string)o[1], 
     Command = (string)o[2] 
    });