我想将json字符串反序列化为c#对象,其格式如下
[{
"data": "{\"SecureId\":null,\"FNAME\":\"WILL\",\"MNAME\":null,\"LNAME\":\"CLINTON\",\"POSTNAME\":\"Mrs.\",\"DOB\":\"6/10/1997\",\"GENDER\":\"M\",\"ORGID\":null}",
"isAvial": true,
"details": "55",
"id": "1999089"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"RIN\",\"MNAME\":null,\"LNAME\":\"MANN\",\"POSTNAME\":\"Jr\",\"DOB\":\"9/16/1997\",\"GENDER\":\"F\",\"ORGID\":null}",
"isAvial": true,
"details": "P2145",
"id": "1999090"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"RIN\",\"MNAME\":null,\"LNAME\":\"MANNING\",\"POSTNAME\":\"M.D.\",\"DOB\":\"9/16/1997\",\"GENDER\":\"F\",\"ORGID\":null}",
"isAvial": true,
"details": "205",
"id": "1999091"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"David\",\"MNAME\":null,\"LNAME\":\"O'PETER\",\"POSTNAME\":\"Dr\",\"DOB\":\"9/6/1997\",\"GENDER\":\"M\",\"ORGID\":null}",
"isAvial": true,
"details": "PM25",
"id": "1999092"}]
我尝试了以下但是它没有奏效。我使用的是Newtonsoft.json
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
public DataTable data { get; set; }
}
public class JsonOutput
{
public List<DataObj> DetailsObj { get; set; }
}
var desc = JsonConvert.DeserializeObject<List<JsonOutput>>(jsonstring);
感谢您的帮助..
答案 0 :(得分:1)
它失败了,因为你的JSON是一个数组但是你试图反序列化为包含数组的对象而不是直接到数组或列表。此外,我严重怀疑您的属性data
可以按原样反序列化,您可能必须编写自定义代码来反序列化DataTable
,或者您应该使用自定义类型将其更改为通用列表/集合
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
// public DataTable data { get; set; } // if this is the FCL defined DataTable you will not be able to deserialize it without some custom code. Better would be to have a strongly typed custom class
}
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
答案 1 :(得分:1)
如果“数据”中的格式正确。然后这将解决
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
public DataTable data { get; set; }
}
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
否则单独解析Datatable或类似的东西 (DataTable)JsonConvert.DeserializeObject(JsonString,(typeof(DataTable)));
答案 2 :(得分:0)
在您的JSON对象中,您有一个DataObj对象列表,因此(如果根据JSON结构正确描述了DataTable类),您应该使用以下内容进行反序列化:
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
答案 3 :(得分:0)
尝试
public class dataC
{
public int SecureId { get; set; }
public string FNAME { get; set; }
public string MNAME { get; set; }
public string LNAME { get; set; }
public string POSTNAME { get; set; }
public string DOB { get; set; }
public string GENDER { get; set; }
public int ORGID { get; set; }
}
public class DataObj
{
public dataC data { get; set; }
public bool isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
}
JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);