2016-02-11 153 views
2

我在將一個JSON對象反序列化到一個類(使用JSON.NET)時遇到了一些麻煩,並希望有人能指出我朝着正確的方向。下面是代碼,我想一個片段,並在dotnetfiddle將JSON對象反序列化爲類

這裏被測試的The JSON的示例:

{ 
    "`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 = "{\"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}"; 
     Console.WriteLine(json); 
     Console.WriteLine(); 

     //This works 
     Console.Write("Deserialize without class"); 
     var root = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(json); 
     foreach (var locationKvp in root) 
     { 
      foreach (var skuKvp in locationKvp.Value) 
      { 
       Console.WriteLine("location: " + locationKvp.Key + ", sku: " + skuKvp.Key + ", qty: " + skuKvp.Value); 
      } 
     } 

     //Why doesn't this work? 
     Console.Write("\nDeserialize with class");  
     var root2 = JsonConvert.DeserializeObject<InventoryLocations>(json); 
     foreach (var locationKvp in root2.InventoryLocation) 
     { 
      foreach (var skuKvp in locationKvp.Value) 
      { 
       Console.WriteLine("location: " + locationKvp.Key + ", sku: " + skuKvp.Key + ", qty: " + skuKvp.Value); 
      } 
     } 
    } 
} 

class InventoryLocations 
{ 
    public Dictionary<Location, Dictionary<Sku, Qty>> InventoryLocation { get; set; } 
} 

public class Location 
{ 
    public string location { get; set; } 
} 

public class Sku 
{ 
    public string sku { get; set; } 
} 

public class Qty 
{ 
    public int qty { get; set; } 
} 

爲什麼反序列化成一個類是不行的?我只是不正確地定義類?

+0

你是因爲他們不是相同的反序列化? ('Dictionary >>'與使用類替換字符串(您的JSON與之不匹配) – crashmstr

+0

看起來像第二個示例(不起作用的示例)嵌套在更深的一層在你的第一個例子中,如果你反序列化爲字典<位置,字典>而不是字典<字符串,字典>? –

回答

3

我在這裏看到兩個問題:一個是使用類作爲字典鍵--JSON在那裏有簡單的字符串(並且不能有其他任何東西),所以這是行不通的。

的第二個問題是JSON的反序列化類的工作原理是匹配鍵的屬性 - 所以它轉換像

{ 
    "prop1": "value1", 
    "prop2": "value2" 
} 

到的實例:

public class MyClass { 
    public string prop1 { get; set; } 
    public string prop2 { get; set; } 
} 

你的情況,這不能因爲在你的JSON中所有的鍵都不是有效的屬性名稱。您必須堅持反序列化到字典

+0

啊,這就是我沒有理解的,它會工作嗎?謝謝澄清。 – jared

1

從JSON生成類的方法之一是使用Visual Studio。

導航至Edit -> Paste Special -> Paste JSON As Classes。對於發佈的JSON,將生成以下類。

public class Rootobject 
{ 
    public LCA0001 LCA0001 { get; set; } 
    public LCA0003 LCA0003 { get; set; } 
    public LCA0005 LCA0005 { get; set; } 
} 

public class LCA0001 
{ 
    public string _23225007190002 { get; set; } 
    public string _23249206670003 { get; set; } 
    public string _01365100070018 { get; set; } 
} 

public class LCA0003 
{ 
    public string _23331406670018 { get; set; } 
    public string _24942506670004 { get; set; } 
} 

public class LCA0005 
{ 
    public string _01365100070018 { get; set; } 
} 
+0

一個.. .for顯示如何通過粘貼JSON作爲類來提高工作效率 –

+1

作爲附註:粘貼JSON As Classes只有在編輯代碼文件時纔可見。請參閱此答案:http://stackoverflow.com/a/18527793/ 642579 – Markus

+1

粘貼爲類很好,但對於這個問題似乎沒有任何價值,因爲它看起來像「名稱」是可變的,而不是固定的類名稱和屬性。 – crashmstr