2016-02-12 71 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。