[英]Deserialize multiple JSON arrays into separate C# datasets/datatables using JSON.net (newtonsoft)
Im trying for hours now to get the following two JSON arrays (namely clients and dossiers) into two separate datasets/datatables using Newtonsoft JSON.net in C#: 我正在尝试几个小时,以便使用C#中的Newtonsoft JSON.net将以下两个JSON数组(即客户端和文档)放入两个单独的数据集/数据表中:
{
"status": "OK",
"clients": [
{
"ClientID": "123456",
"Fullname": "John Doe",
"Inactive": false
},
{
"ClientID": "234567",
"Fullname": "James Smith",
"Inactive": false
}
],
"dossiers": [
{
"CreateDate": "03.06.2013",
"DossierName": "JD20130603"
},
{
"CreateDate": "04.06.2013",
"DossierName": "JS20130604"
}
]
}
Can someone please help? 有人可以帮忙吗? Thanks in advance... 提前致谢...
EDIT: I would like to avoid the whole Class thing if possible. 编辑:如果可能的话,我想避免整个Class的事情。
EDIT 2: So far Ive tried the following approaches 编辑2:到目前为止,我已经尝试了以下方法
var _clientlist = JObject.Parse(_jsonresp)["clients"].Children();
Which works, but I cannot get the values into a dataset/datable 哪个可行,但我无法将值放入数据集/可重复
_clientlist = (DataTable)JsonConvert.DeserializeObject(_jsonresp, (typeof(DataTable)));
Fails :( 失败:(
DataSet _dataset = JsonConvert.DeserializeObject<DataSet>(_jsonresp);
DataTable _clientlist = _dataset.Tables["clients"];
Similar process to above but same result 与上述过程类似,但结果相同
dynamic _d = JValue.Parse(_response);
JArray _jsonval = JArray.Parse(_d.clients) as JArray;
Fails :( 失败:(
At which point I gave up. 在这一点上,我放弃了。
This doesn't answer the question exactly because I personally don't see why you would want to deserialize into datasets when the json.NET model is more oriented around deserializing into objects. 这并不能完全回答问题,因为我个人不知道为什么当json.NET模型更注重于反序列化为对象时,为什么要反序列化为数据集。 Here it is; 这里是;
public class TheJson
{
public string status { get; set; }
public client[] clients { get; set; }
public dossier[] dossiers { get; set; }
}
public class client
{
public string ClientID { get; set; }
public string Fullname { get; set; }
public bool Inactive { get; set; }
}
public class dossier
{
public string CreateDate { get; set; }
public string DossierName { get; set; }
}
With those definitions it's as simple as; 有了这些定义,它就很简单;
TheJson clientsAndDossiers = JsonConvert.DeserializeObject<TheJson>(_jsonresp);
Now with regard to your last comment, to apply search filters I would just use LINQ. 现在,关于您的最后一条评论,要应用搜索过滤器,我将仅使用LINQ。 Like for example if I want to get only active clients I could do; 例如,如果我只想获得活跃的客户,那我可以做;
List<client> activeClients = clientsAndDossiers.clients.Where(x => x.Inactive == false).ToList();
With regard your comment on this post, here is the LINQ implementation; 关于您对本文的评论,这是LINQ的实现;
string inputString = MenuSearchBox.Text.ToString();
List<client> filtered = clientsAndDossiers.clients.Where(x => x.Fullname.Contains(inputString)).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.