2014-06-25 55 views
2

我們有一個JSON結構,我們從服務器向客戶端發送一個鍵值對的數組。爲了簡潔和整齊,我們發送的數據是這樣的:作爲數組的反序列化JSON對象

"identification": { 
    "lineItem/latestDate": "2013-04-28", 
    "lineItem/destination": "test", 
    "lineItem/quantity": "55" 
} 

,而不是這樣的:

"identification": [ 
    { 
     "value": "test", 
     "type": "lineItem/destination" 
    }, 
    { 
     "value": "2014-07-25", 
     "type": "lineItem/latestDate" 
    }, 
    { 
     "value": "55", 
     "type": "lineItem/quantity" 
    } 
] 

的問題是我們的下游客戶(誰使用C#處理JSON)說,他們無法處理第一種格式,因爲內置的json.net功能不支持它,並且他們不知道另一個可以做到這一點。

是否有可能使用C#中內置的JSON反序列化反序列化第一種格式?還是有人知道可以照顧這個的另一個圖書館?我看過這個question,對我來說看起來可能。

回答

5

在這裏,我發現這樣做的另一種方式和好消息我們是否需要更改關鍵名稱?

在客戶端創建一個類:

public class RootObject 
{ 
    public Dictionary<string,string> identification { get; set; } 
} 

現在,反序列化:

 string data = @"[ 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}, 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}, 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}]"; 

List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data); 

賓果,我們已經做了一遍。

1)現在,ob對象具有期望的值。 2)與數組數組一起工作。

3)也支持迭代。

4

1)給定第一個JSON無效,需要在開始時使用左大括號,並從最後一個逗號中刪除一個逗號。

2)如果您的客戶端可以使用Newtonsoft.Json,你可以用「_」,而不是「/」的鑰匙,因爲內的名稱以「/」標識不是在C#

客戶端支持:

創建類的

public class Identification 
{ 
    public string lineItem_latestDate { get; set; } 
    public string lineItem_destination { get; set; } 
    public string lineItem_quantity { get; set; } 
} 

public class RootObject 
{ 
    public Identification identification { get; set; } 
} 

注意:如果你有更多的鍵/值對,您可以來上課標識添加更多屬性現在

,反序列化的:

 string data = @"[ 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}, 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}, 
{ 
    ""identification"": { 
     ""lineItem_latestDate"": ""2013-04-28"", 
     ""lineItem_destination"": ""test"", 
     ""lineItem_quantity"": ""55"" 
    } 
}]"; 

     List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data); 

完成,現在ob對象具有根據數據的值。

+0

不應該RootObject包含public List identification {get; set;}? 根據標題,JSON字符串應該作爲數組反序列化。 –

+0

感謝您注意到,我不知道我是如何跳過這一點的。 如果它是一個數組,它應該是List ,我會更新它 – Dev

+0

未編碼的lineItems會發生什麼?即如果消費者想要迭代這些項目? – Aphelion

2

Json.Net不支持反序列化鍵值配對:你只需要使用一個Dictionary<string, T>,其中T可以objectJToken或可接收的數據值(string作品在您的情況),其他一些類。藉助字典,它實際上是很容易的反序列化和迭代值,如下圖所示:

class Program 
{ 
    static void Main(string[] args) 
    { 
     string json = @" 
     { 
      ""identification"": { 
       ""lineItem/latestDate"": ""2013-04-28"", 
       ""lineItem/destination"": ""test"", 
       ""lineItem/quantity"": ""55"", 
      } 
     }"; 

     RootObject obj = JsonConvert.DeserializeObject<RootObject>(json); 

     foreach (KeyValuePair<string, string> kvp in obj.identification) 
     { 
      Console.WriteLine("type: " + kvp.Key); 
      Console.WriteLine("value: " + kvp.Value); 
      Console.WriteLine(); 
     } 
    } 
} 

class RootObject 
{ 
    public Dictionary<string, string> identification { get; set; } 
} 

輸出:

type: lineItem/latestDate 
value: 2013-04-28 

type: lineItem/destination 
value: test 

type: lineItem/quantity 
value: 55 
+0

刪除我的帖子,因爲這個新的信息證明我錯了。謝謝。 – Aphelion