C# 将Json转换为列表<;对象>;使用.NET
在尝试将Json转换为对象列表的几天后,我来到了这里。 我有一个返回Json字符串的REST API:C# 将Json转换为列表<;对象>;使用.NET,c#,.net,json,C#,.net,Json,在尝试将Json转换为对象列表的几天后,我来到了这里。 我有一个返回Json字符串的REST API: { GetItemsListResult:"[ { "code":"VOL00056", "clsID":223108653, "type":2, "status":1, "free":0.0, "total":671088640.0, "perc":
{
GetItemsListResult:"[
{
"code":"VOL00056",
"clsID":223108653,
"type":2,
"status":1,
"free":0.0,
"total":671088640.0,
"perc":99,
"descr":"no mailing",
"dt":20160926,
"tm":112456,
"full":1
},
{
"code":"VOL00055",
"clsID":111760419,
"type":2,
"status":1,
"free":0.0,
"total":671088640.0,
"perc":99,
"descr":"Email",
"dt":20160817,
"tm":222411,
"full":1
}
]"
}
我知道这个字符串来自数据表:
String JSONresult = JsonConvert.SerializeObject(ds.Tables[0]);
我创建了两个类:一个描述对象模型,另一个获取集合。但是当你试图
VolumeCollection volumes = Newtonsoft.Json.JsonConvert.DeserializeObject<VolumeCollection>(listVolumes);
接收到的json字符串是:
"\"
[{\\“FreeSpace\\”:0.0,\\\“TotalSpace\\”:671088640.0,\\“FillPercentage\\”:99,\\“ClassDescription\\”:\\“Email esterne\\”,\\“CloseDate\\”:20161001,\\“CloseTime\\”:212512,\\“IsFull\\”:true,\\“VolumeType\\”:2,\\“ClassId\\”:111760419,\\“Code\\”:“VOL00057\\,“Status\\”:1},
{\\“FreeSpace\\”:0.0,\\“TotalSpace\\”:671088640.0,\\“FillPercentage\\”:99,\\“ClassDescription\\”:\\“Corrispondenza no Mailding\\”,\\“CloseDate\\”:20160926,\\“CloseTime\\”:112456,\\“IsFull\\”:true,\\“VolumeType\\”:2,\\“ClassId\\”:223108653,\\“Code\\”:\“VOL00056\”,“Status\\”:1}
]\“”
它似乎是无效的json…鉴于您的
json
无效,并且鉴于以下情况:
{
"GetItemsListResult": [{
"code": "VOL00056",
"clsID": 223108653,
"type": 2,
"status": 1,
"free": 0.0,
"total": 671088640.0,
"perc": 99,
"descr": "no mailing",
"dt": 20160926,
"tm": 112456,
"full": 1
}, {
"code": "VOL00055",
"clsID": 111760419,
"type": 2,
"status": 1,
"free": 0.0,
"total": 671088640.0,
"perc": 99,
"descr": "Email",
"dt": 20160817,
"tm": 222411,
"full": 1
}]
}
使用我生成的以下类,这是可行的:
public class GetItemsListResult
{
public string code { get; set; }
public int clsID { get; set; }
public int type { get; set; }
public int status { get; set; }
public double free { get; set; }
public double total { get; set; }
public int perc { get; set; }
public string descr { get; set; }
public int dt { get; set; }
public int tm { get; set; }
public int full { get; set; }
}
public class RootObject
{
public List<GetItemsListResult> GetItemsListResult { get; set; }
}
公共类GetItemsListResult
{
公共字符串代码{get;set;}
public int clsID{get;set;}
公共int类型{get;set;}
公共int状态{get;set;}
公共双自由{get;set;}
公共双总计{get;set;}
公共int perc{get;set;}
公共字符串descr{get;set;}
公共int dt{get;set;}
公共int tm{get;set;}
public int full{get;set;}
}
公共类根对象
{
公共列表GetItemsListResult{get;set;}
}
var res=JsonConvert.DeserializeObject(json)代码>
注意:这个站点从
JSON
:生成的类是一个字符串,而不是数组。请注意双引号:
GetItemsListResult: "[
因此,理想情况下,您希望将json修复为真正的数组
如果您不能控制json,那么作为一个糟糕的选择,您可以递归地解析字符串以提取数组
{
"GetItemsListResult":[
{"code":"VOL00056","clsID":223108653,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"no mailing","dt":20160926,"tm":112456,"full":1},
{"code":"VOL00055","clsID":111760419,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"Email","dt":20160817,"tm":222411,"full":1}
]
}
public class GetItemsListResult
{
public string code { get; set; }
public int clsID { get; set; }
public int type { get; set; }
public int status { get; set; }
public double free { get; set; }
public double total { get; set; }
public int perc { get; set; }
public string descr { get; set; }
public int dt { get; set; }
public int tm { get; set; }
public int full { get; set; }
}
public class RootObject
{
public List<GetItemsListResult> GetItemsListResult { get; set; }
}
var jsonObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
{
"GetItemsListResult":[
{"code":"VOL00056","clsID":223108653,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"no mailing","dt":20160926,"tm":112456,"full":1},
{"code":"VOL00055","clsID":111760419,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"Email","dt":20160817,"tm":222411,"full":1}
]
}
public class GetItemsListResult
{
public string code { get; set; }
public int clsID { get; set; }
public int type { get; set; }
public int status { get; set; }
public double free { get; set; }
public double total { get; set; }
public int perc { get; set; }
public string descr { get; set; }
public int dt { get; set; }
public int tm { get; set; }
public int full { get; set; }
}
public class RootObject
{
public List<GetItemsListResult> GetItemsListResult { get; set; }
}
var jsonObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
公共类GetItemsListResult
{
公共字符串代码{get;set;}
public int clsID{get;set;}
公共int类型{get;set;}
公共int状态{get;set;}
公共双自由{get;set;}
公共双总计{get;set;}
公共int perc{get;set;}
公共字符串descr{get;set;}
公共int dt{get;set;}
公共int tm{get;set;}
public int full{get;set;}
}
公共类根对象
{
公共列表GetItemsListResult{get;set;}
}
{
"GetItemsListResult":[
{"code":"VOL00056","clsID":223108653,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"no mailing","dt":20160926,"tm":112456,"full":1},
{"code":"VOL00055","clsID":111760419,"type":2,"status":1,"free":0.0,"total":671088640.0,"perc":99,"descr":"Email","dt":20160817,"tm":222411,"full":1}
]
}
public class GetItemsListResult
{
public string code { get; set; }
public int clsID { get; set; }
public int type { get; set; }
public int status { get; set; }
public double free { get; set; }
public double total { get; set; }
public int perc { get; set; }
public string descr { get; set; }
public int dt { get; set; }
public int tm { get; set; }
public int full { get; set; }
}
public class RootObject
{
public List<GetItemsListResult> GetItemsListResult { get; set; }
}
var jsonObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
var jsonObj=JsonConvert.DeserializeObject(jsonString);
您可以共享您的VolumeCollection
类吗?公共类VolumeCollection{public List GetItemsListResult{get;set;}}您的json字符串无效。您可以共享卷
类吗?最好将其添加到您的问题中。以及VolumeCollection
类。您的JSON字符串无效。这就是它不能反序列化的原因。你能发布生成JSON的控制器吗?这部分有点问题。好吧,假设json来自一个数据表,我可以更改它吗?我可以用一个有意义的名称重命名GetItemsListResult吗?我不确定我是否理解你的意思,你是说你不能控制json的创建方式吗?或多或少。。。我可以“说服”准备json的人:-)数据来自数据库表。如何更改数据的准备工作?与其首先使用DataTable,不如从表的每一行创建对象,然后将其序列化为json
,这样更易于维护。好的,与其使用DataTable,不如使用DataReader循环并创建对象列表。对吗?