我希望将下面的json字符串反序列化为3个不同的DataGrids。一个用于结果,一个用于提款,一个用于存款。有人有这么好的方法吗?任何帮助将非常感激。
{
"results": [
{
"id": "51142254",
"tp_id": "!XP4D49X0CD123628",
"firstname": "Owner",
"lastname": "Operator",
"email": "",
"phone": "",
"enrolled": "1",
"balance": 247.54,
"fleet": "Test Express",
"deposits": [
{
"id": "184022380",
"date": "2016-02-17",
"amount": "200.00",
"transID": "135246",
"memo": "Scheduled Deposit",
"status": "Cleared"
},
{
"id": "184022383",
"date": "2016-02-25",
"amount": "200.00",
"transID": "246357",
"memo": "Scheduled Deposit",
"status": "Cleared"
},
{
"id": "184022386",
"date": "2016-03-02",
"amount": "200.00",
"transID": "975468",
"memo": "Scheduled Deposit",
"status": "Cleared"
}
],
"withdrawals": [
{
"id": "184026949",
"date": "2016-03-09",
"amount": "352.46",
"transID": "395920",
"memo": "Invoice\r\n\r\n100234",
"status": "Cleared"
}
]
},
{
"id": "51142326",
"tp_id": "!XP4D49X7CD123612",
"firstname": "Owner",
"lastname": "Operator",
"email": "",
"phone": "",
"enrolled": "1",
"balance": 0,
"fleet": "Test\r\nExpress",
"deposits": [],
"withdrawals": []
}
]
}
当我去json2chsarp.com时,这就是为我的课程生成的内容。我假设存款和取款部分没有显示,因为这里没有列出子节点。该如何正确完成?
public class Result
{
public string id { get; set; }
public string tp_id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string enrolled { get; set; }
public double balance { get; set; }
public string fleet { get; set; }
public List<object> deposits { get; set; }
public List<object> withdrawals { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
答案 0 :(得分:1)
您必须创建一个类Results并定义您的JSON具有的每个属性。此外,您还必须为存款和提款定义一个类,并在结果类中添加一个属性列表和列表< / strong>即可。然后你可以使用这个
var results = new JavaScriptSerializer().Deserialize<List<results>>();
希望这有帮助
在此之后,您可以使用对象填充数据网格
答案 1 :(得分:1)
首先,我建议创建一个直接映射到Json字符串中给出的对象和属性的对象(以及后续的对象)。
然后您可以使用JsonConvert.DeserializeObject<T>("your json string here")
;其中T
是您的新对象类型,将其转换回新对象的实例。
不要忘记在课堂上使用Newtonsoft.Json
。
转到Newtonsoft's Website,看看如何做到这一点/如何运作。
我也会看一下another useful answer,并提出类似的问题。
希望这有帮助。
答案 2 :(得分:1)
Newtonsoft JSON library是处理JSON对象的绝佳选择。使用JObject.Parse的动态功能,您可以在解析它之后从JSON对象中提取数据(其中rawJson是您的JSON字符串):
dynamic jsonBody = JObject.Parse(rawJson);
dynamic results = jsonBody.results;
循环遍历结果数组,您可以获取插入数据网格所需的所有字段和数据:
foreach(dynamic curResult in results)
{
JArray deposits = (JArray)curResult.deposits;
JArray withdrawals = (JArray)curResult.withdrawals;
//To get column names and values for your first data grid, iterate over the properties within curResult record
JObject header = (JObject)curResult;
var headerObjects = header.Properties().
Find(x =>
!x.Name.Equals("deposits") ||
!x.Name.Equals("withdrawals"));
}
您可以使用每个结果中的deposit数组和withdrawals数组中的记录来填充这些数据网格。
这只是此库允许您迭代JSON文档的众多方法之一。我希望这有用。
答案 3 :(得分:0)
public class Deposite
{
public int id { get; set; }
public date date { get; set; }
public double amount { get; set; }
public int transID { get; set; }
public string Memo { get; set; }
public string status { get; set; }
}
public class Withdrawal
{
public int id { get; set; }
public date date { get; set; }
public double amount { get; set; }
public int transID { get; set; }
public string Memo { get; set; }
public string status { get; set; }
}
public class results
{
public int id { get; set; }
public int tp_id{ get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string enrolled { get; set; }
public string balance { get; set; }
public string fleet { get; set; }
public Deposite[] deposite { get; set; }
public Withdrawal[] withdrawal { get; set; }
}
public class opt
{
public results[] response { get; set; }
}
using System.Web.Script.Serialization;
JavaScriptSerializer objJS = new JavaScriptSerializer();
opt objopt = new opt();
objopt = objJS .Deserialize<opt >("Your String");