2016-02-12 36 views
4

当有空/空属性(使用JSON.NET)时,我在反序列化JSON对象时遇到了一些麻烦,并希望有人能指引我朝着正确的方向发展。下面是代码,我想一个片段,并在dotnetfiddle使用空数组反序列化JSON对象

这里被测试的The JSON的示例:

{ 
    "`LCA0009": [], 
    "`LCA0001": { 
     "23225007190002": "1", 
     "23249206670003": "1", 
     "01365100070018": "5" 
    }, 
    "`LCA0003": { 
     "23331406670018": "1", 
     "24942506670004": "1" 
    }, 
    "`LCA0005": { 
     "01365100070018": "19" 
    } 
} 

我想使用此代码:

using System; 
using System.Collections.Generic; 
using Newtonsoft.Json; 

public class Program 
{ 
    public static void Main() 
    { 

     string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}"; 
     Console.WriteLine(json); 
     Console.WriteLine(); 

     Console.WriteLine("This works"); 
     var root = JsonConvert.DeserializeObject(json); 
     Console.WriteLine(root); 

     Console.WriteLine("This doesn't work"); 
     var root2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(json); 
     Console.WriteLine(root2); 

     foreach (var locationKvp in root2) 
     { 
      foreach (var skuKvp in locationKvp.Value) 
      { 
       Console.WriteLine("location: " + locationKvp.Key + ", sku: " + skuKvp.Key + ", qty: " + skuKvp.Value); 
      } 
     } 
    } 
} 

在“不工作”上面的是,我得到这个错误:

Run-time exception (line 19): Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '`LCA0009', line 1, position 14.

我如何删除properti es有一个空/空数组?

+1

这里存在的根本问题是JSON不一致,给你一些数组作为一些条目,但为其他对象。解决这个问题的最好的地方就是生成JSON的地方:而不是LCA0009:[]',它应该是''LCA0009“:{}'。 –

+0

修复JSON [解决问题](https://dotnetfiddle.net/K7wtnh)。 –

+0

@ T.J。 Crowder - 不幸的是,我不控制消息来源。我想我可以通过一些字符串替换函数将[]转换为{},这将作为一种工作方式,直到我修改了源文件。 – jared

回答

3

基本上,JSON不一致,最好的答案是使其一致。无论谁给你JSON需要被告知修复它。

直到/除非你能做到这一点,你可以在“原始”版转换Dictionary<string, Dictionary<string, int>>自己,但它更多的工作:

https://dotnetfiddle.net/zdeOOX

string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}"; 

// Convert it 
var root = (JObject)JsonConvert.DeserializeObject(json); 
var results = new Dictionary<string, Dictionary<string,int>>(); 
foreach (var entry in root) 
{ 
    var dict = new Dictionary<string,int>(); 
    if (!(entry.Value is JArray)) 
    { 
     foreach (var subentry in (JObject)entry.Value) 
     { 
      int v; 
      if (int.TryParse(((JValue)subentry.Value).ToString(), out v)) 
      { 
       dict.Add(subentry.Key, v); 
      } 
     } 
    } 
    results.Add(entry.Key, dict); 
} 

// Results: 
foreach (var name in results.Keys) 
{ 
    var entry = results[name]; 
    Console.WriteLine(name + ":"); 
    foreach (var entryKey in entry.Keys) 
    { 
     Console.WriteLine("- " + entryKey + ": " + entry[entryKey]); 
    } 
} 

我希望可以做出更优雅与Linq。