将Json转换为List< object>与.NET [英] Convert Json to List<object> with .NET
问题描述
几天后尝试将Json转换为对象列表,我在这里. 我有一个返回Json字符串的REST API:
After days to try to convert Json to List of objects, I'm here. I have a REST API that return the Json string:
{
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
}
]"
}
我知道此字符串来自数据表:
I know that this string comes from a DataTable:
String JSONresult = JsonConvert.SerializeObject(ds.Tables[0]);
我创建了两个类:一个描述对象模型,另一个描述获取集合.但是当尝试
I created two class: one that describe the object model and another that get the collection. But when trying to
VolumeCollection volumes = Newtonsoft.Json.JsonConvert.DeserializeObject<VolumeCollection>(listVolumes);
我获得 无法将System.String强制转换或转换为System.Collections.Generic.List`1 [卷].
I obtain Could not cast or convert from System.String to System.Collections.Generic.List`1[Volume].
怎么了?
音量等级:
public class Volume
{
public String code { get; set; }
public Int32 classId { get; set; }
public Byte volumeType { get; set; }
public Byte status { get; set; }
public float freeSpace { get; set; }
public float totalSpace { get; set; }
public Int16 fillPercentage { get; set; }
public String classDescription { get; set; }
public Int32 closeDate { get; set; }
public Int32 closeTime { get; set; }
public Boolean isFull { get; set; }
}
收到的json字符串是: "\" [{\\"FreeSpace \\":0.0,\\"TotalSpace \\":671088640.0,\\"FillPercentage \\":99,\\"ClassDescription \\":\\通过电子邮件发送esterester \\",\ \"CloseDate \\":20161001,\\"CloseTime \\":212512,\\"IsFull \\":true,\\"VolumeType \\":2,\\"ClassId \\":111760419,\ \代码\\":\\"VOL00057 \\",\\状态\\":1}, {\\"FreeSpace \\":0.0,\\"TotalSpace \\":671088640.0,\\"FillPercentage \\":99,\\"ClassDescription \\":\\"Corrispondenza no mailing \\",\ \"CloseDate \\":20160926,\\"CloseTime \\":112456,\\"IsFull \\":true,\\"VolumeType \\":2,\\"ClassId \\":223108653,\ \代码\\":\\"VOL00056 \\",\\状态\\":1} ] \"
The json string received is: "\" [{\\"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 mailing\\",\\"CloseDate\\":20160926,\\"CloseTime\\":112456,\\"IsFull\\":true,\\"VolumeType\\":2,\\"ClassId\\":223108653,\\"Code\\":\\"VOL00056\\",\\"Status\\":1} ]\""
似乎是无效的json ...
It seems to be a not valid json...
推荐答案
鉴于您的json
无效,并且假定以下内容为:
Given that your json
is not valid and given that the following is:
{
"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
}]
}
使用我生成的以下类,它可以工作:
using the following classes that I generated, this works:
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 res = JsonConvert.DeserializeObject<RootObject>(json);
N.B:此站点从JSON
生成了类: json2csharp
N.B: this site generated the classes from the JSON
: json2csharp
这篇关于将Json转换为List< object>与.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!