C# 在列表中使用Newtonsoft列表反序列化对象
首先,对不起我的英语不好。大家好,我正在尝试将json(使用newtonsoft)反序列化为一个列表,这非常有效。但我唯一的问题是,如果可能的话,我需要把一个列表放在一个列表中。之所以要这样做,是因为我有一组包含子项的项。我该如何把它们都放在一个很好的排序列表中?下面是我编写的一些示例代码: C#代码C# 在列表中使用Newtonsoft列表反序列化对象,c#,.net,json,winforms,C#,.net,Json,Winforms,首先,对不起我的英语不好。大家好,我正在尝试将json(使用newtonsoft)反序列化为一个列表,这非常有效。但我唯一的问题是,如果可能的话,我需要把一个列表放在一个列表中。之所以要这样做,是因为我有一组包含子项的项。我该如何把它们都放在一个很好的排序列表中?下面是我编写的一些示例代码: C#代码 var items = JsonConvert.DeserializeObject<List<Items>>(wc.DownloadString("http://local
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; }
}
[
{
"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
}
]
}
]
我的问题是,我似乎无法将第二个数组放在包含项的列表中。我知道我做错了什么,但我似乎不知道是什么,有人能帮我吗?非常感谢。在json中,版本是一个数组。您还必须对该对象建模 你的模型应该是这样的
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; }
}
公共类项目
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共字符串说明{get;set;}
公共列表版本{get;set;}
}
公共类版本
{
公共字符串Appid{get;set;}
公共字符串版本{get;set;}
公共字符串补丁_注释{get;set;}
公共字符串下载链接{get;set;}
公共整数大小{get;set;}
}
在json中,版本是一个数组。您还必须对该对象建模
你的模型应该是这样的
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; }
}
公共类项目
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共字符串说明{get;set;}
公共列表版本{get;set;}
}
公共类版本
{
公共字符串Appid{get;set;}
公共字符串版本{get;set;}
公共字符串补丁_注释{get;set;}
公共字符串下载链接{get;set;}
公共整数大小{get;set;}
}
谢谢!这似乎很有效。我现在明白了,谢谢!非常感谢。这似乎很有效。我现在明白了,谢谢!