2016-06-07 50 views
-3

我想將jsonarray反序列化爲對象列表。使用包裝類將JsonArray轉換爲模型

JSON字符串是在這裏:

[{ 
    id: 1, 
    customer: "Joe Black", 
    items: { 
     id: 1, 
     description: "One", 
     unit_price: 1.00, 
     quantity: 1 
    } 
},{ 
    id: 2, 
    customer: "Joe", 
    items: { 
     id: 2, 
     description: "two", 
     unit_price: 1.00, 
     quantity: 4 
    } 
}] 

JsonConvert.DeserializeObject<List<rootClass>>(jsonString) 

更新:這是我的班

public class customer { 
    public int id {get; set;} 
    public string customer {get; set;} 
    public Item item {get; set;} 
} 

public class Item { 
    public string id {get; set;} 
    public string description {get; set;} 
    public int unit_price {get; set;} 
    public int quantity {get; set;} 
} 
+0

你使用什麼編程語言? – JJJ

+0

@Juhana看來,作者使用C# – reporter

+0

@Juhana我使用c# – user2011138

回答

1

使用像http://jsonutils.com/一個工具,你可以在你的JSON和粘貼將生成你的類看起來像(只要JSON是有效的)。

解析您提供的JSON產生了以下

public class Items 
{ 
    public int id { get; set; } 
    public string description { get; set; } 
    public double unit_price { get; set; } 
    public int quantity { get; set; } 
} 

public class rootClass 
{ 
    public int id { get; set; } 
    public string customer { get; set; } 
    public Items items { get; set; } 
} 

從你的類定義不同。

具體爲您的customer類中的Item類和item屬性。

您需要檢查數據並確保它與您的班級結構相匹配。

+0

我我將項目設置爲null – user2011138

+0

在您與班級發佈的評論中,我注意到您擁有「item」(單數)屬性'items'(複數),這不符合您的JSON – Nkosi

+0

是項目單一 – user2011138

相關問題