2015-04-17 69 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] 
    });