2013-06-20 60 views
1

首先,對不起我的英文不好。您好,我試圖反序列化JSON(與牛頓軟件)到一個列表,這很好。但是我唯一的問題是我需要在列表中添加一個列表,如果這是可能的話。爲什麼我想這樣做是因爲我有一系列有子項的項目。我怎麼把他們全部放在一個很好的排序列表中?下面是一些示例代碼,我提出:DeserializeObject列表中的Newtonsoft列表

C#代碼

var items = JsonConvert.DeserializeObject<List<Items>>(wc.DownloadString("http://localhost/index.php")); 
foreach (var item in items) 
{ 
    Console.WriteLine(item); 
} 

listItems.AddRange(items); 

public class Items 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Genre { get; set; } 
    public string Size { get; set; } 
    public string Version { get; set; } 
    public string Download_Link { get; set; } 
    public string Description { get; set; } 
} 

JSON

[ 
    { 
     "id": "1", 
     "name": "Application 1", 
     "genre": "Something", 
     "description": "The description", 
     "versions": [ 
      { 
       "appid": "1", 
       "version": "1", 
       "patch_notes": "Release version.", 
       "download_link": "http://localhost/downloads/application_1.zip", 
       "size": 5120 
      } 
     ] 
    } 
] 

我的問題是,我似乎不能把第二陣列的內部與項目列表。我知道我做錯了什麼,但我似乎無法弄清楚什麼,有人可以幫助我嗎?這將非常感激。

回答

1

在json中,Versions是一個數組。你也必須建模該對象。

你的模型應該是這個樣子

public class Items 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public string Genre { get; set; } 
    public string Description { get; set; } 
    public List<Version> Versions { get; set; } 
} 

public class Version 
{ 
    public string Appid { get; set; } 
    public string Version { get; set; } 
    public string Patch_Notes { get; set; } 
    public string Download_Link { get; set; } 
    public int Size { get; set; } 
} 
+0

謝謝!這似乎是完美的。我現在明白了,謝謝^ _ ^! –