Linq查询JObject
我正在使用Json.net进行序列化,然后创建一个如下所示的JObject:
"RegistrationList": [ { "CaseNumber": "120654-1330", "Priority": 5, "PersonId": 7, "Person": { "FirstName": "", "LastName": "", }, "UserId": 7, "User": { "Id": 7, "CreatedTime": "2013-07-05T13:09:57.87", "Comment": "", },
我如何查询到一个新的对象或列表,这很容易放入一些html表/视图。 我只想显示CaseNumber,FirstName和Comment。
我只想显示CaseNumber,FirstName和Comment。
与ASP.NET MVC一样,您可以从编写符合您要求的视图模型开始:
public class MyViewModel { public string CaseNumber { get; set; } public string FirstName { get; set; } public string Comment { get; set; } }
然后在您的控制器操作中,您从已有的JObject实例构建视图模型:
public ActionResult Index() { JObject json = ... the JSON shown in your question (after fixing the errors because what is shown in your question is invalid JSON) IEnumerable model = from item in (JArray)json["RegistrationList"] select new MyViewModel { CaseNumber = item["CaseNumber"].Value(), FirstName = item["Person"]["FirstName"].Value (), Comment = item["User"]["Comment"].Value (), }; return View(model); }
最后在强类型视图中显示所需信息:
@model IEnumerable Case number First name Comment @foreach (var item in Model) { @item.CaseNumber @item.FirstName @item.Comment }
几种方式:
1)根据文档’ 使用LINQ for JSON ‘,你可以用LINQ方式查询JObject
JObject o = JObject.Parse(@"{ 'CPU': 'Intel', 'Drives': [ 'DVD read/writer', '500 gigabyte hard drive' ] }"); string cpu = (string)o["CPU"]; // Intel string firstDrive = (string)o["Drives"][0]; // DVD read/writer IList allDrives = o["Drives"].Select(t => (string)t).ToList(); // DVD read/writer // 500 gigabyte hard drive
2) 使用SelectToken查询JSON
3)使用自定义帮助程序扩展方法按指定路径查询,如下所示:
public static class JsonHelpers { public static JToken QueryJson(this object jsonObject, params string[] jsonPath) { const string separator = " -> "; if (jsonObject == null) throw new Exception(string.Format("Can not perform JSON query '{0}' as the object is null.", string.Join(separator, jsonPath ?? new string[0]))); var json = (jsonObject as JToken) ?? JObject.FromObject(jsonObject); var token = json; var currentPath = ""; if (jsonPath != null) foreach (var level in jsonPath) { currentPath += level + separator; token = token[level]; if (token == null) break; } if (token == null) throw new Exception(string.Format("Can not find path '{0}' in JSON object: {1}", currentPath, json)); return token; } }
我想你想得到如下的JSON字符串:
{ 'RegistrationList': [ { 'CaseNumber': '120654-1330', 'Priority': 5, 'PersonId': 7, 'Person': { 'FirstName': '0', 'LastName': '', }, 'UserId': 7, 'User': { 'Id': 7, 'CreatedTime': '2013-07-05T13:09:57.87', 'Comment': '' } }, { 'CaseNumber': '120654-1330', 'Priority': 5, 'PersonId': 7, 'Person': { 'FirstName': '0', 'LastName': '', }, 'UserId': 7, 'User': { 'Id': 7, 'CreatedTime': '2013-07-05T13:09:57.87', 'Comment': '' } }, ] }
如果是这样,您可以获得以下代码来处理您的问题:
上述就是C#学习教程:Linq查询JObject分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
string json = @"{ 'RegistrationList': [ { 'CaseNumber': '120654-1330', 'Priority': 5, 'PersonId': 7, 'Person': { 'FirstName': '0', 'LastName': '', }, 'UserId': 7, 'User': { 'Id': 7, 'CreatedTime': '2013-07-05T13:09:57.87', 'Comment': '' } }, { 'CaseNumber': '120654-1330', 'Priority': 5, 'PersonId': 7, 'Person': { 'FirstName': '0', 'LastName': '', }, 'UserId': 7, 'User': { 'Id': 7, 'CreatedTime': '2013-07-05T13:09:57.87', 'Comment': '' } }, ] }"; JObject o = JObject.Parse(json); JArray list = (JArray)o["RegistrationList"]; List> rList = new List>(); foreach (var r in list) { Tuple temp = new Tuple(r["CaseNumber"].Value(), r["Person"]["FirstName"].Value (), r["User"]["Comment"].Value ()); rList.Add(temp); Console.WriteLine(temp); }
var serializer = new JavaScriptSerializer(); object modelData = serializer.DeserializeObject(jsonstring);
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1044271.html