LINQ到JSON选择某些元素 [英] LINQ to JSON selection of certain elements

查看:122
本文介绍了LINQ到JSON选择某些元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的JSON:

  {响应:2939,
{中等:6581,日期:1345018696,走出去:0,流体:84175314,read_state:1,称号:......,体:文本1},
{中等:6578,日期:1344984256,走出去:0,流体:32438192,read_state:1,称号:......,体:文本2}
]}
 

使用Newtonsoft JSON库,我需要选择只有这部分(然后将数据添加到我的对象)

{中等:6581,日期:1345018696,走出去:0,流体:84175314,read_state:1,称号:...... 身:文本1},     {中等:6578,日期:1344984256,走出去:0,流体:32438192,read_state:1,称号:......,体:文本2}

(有源JSON超过2元)

我已经写了下面至今:

  JObject的JRE = JObject.Parse(JSON);
JArray罐=(JArray)的JRE [回应];

VAR的查询=
                    味精的罐子
                    选择新
                    {
                        中期=(int)的JAR [中期],
                        日期=(int)的JAR [日期],
                        outt =(短期)的容器[出来],
                        UID =(int)的JAR [UID],
                        read_state =(短期)的容器[read_state],
                        标题=(字符串)JAR [标题],
                        身体=(字符串)JAR [机构],
                    };
 

我想限制查询跳过数组中的第一个对象,但我不知道该怎么做。

解决方案

  VAR的查询=味精的罐子
            在那里!(msg是JValue)
            选择新
            {
                中期=(INT)味精[中期],
                日期=(INT)味精[日期],
                outt =(短期)味精[出来],
                UID =(INT)味精[UID],
                read_state =(短期)味精[read_state],
                标题=(字符串)味精[标题],
                身体=(字符串)味精[机构],
            };
 

I have the following JSON:

{"response":[2939,
{"mid":6581,"date":1345018696,"out":0,"uid":84175314,"read_state":1,"title":" ... ","body":"Text1"},
{"mid":6578,"date":1344984256,"out":0,"uid":32438192,"read_state":1,"title":" ... ","body":"Text2"}
]}

Using the Newtonsoft JSON library, I need to select only this part (and then add the data to my object)

{"mid":6581,"date":1345018696,"out":0,"uid":84175314,"read_state":1,"title":" ... ","body":"Text1"}, {"mid":6578,"date":1344984256,"out":0,"uid":32438192,"read_state":1,"title":" ... ","body":"Text2"}

(There are more than 2 elements in the source JSON)

I've written the following so far:

JObject jRes = JObject.Parse(json);
JArray jAr = (JArray)jRes["response"];

var query =
                    from msg in jAr
                    select new 
                    {
                        mid = (int)jAr["mid"],
                        date = (int)jAr["date"],
                        outt = (short)jAr["out"],
                        uid = (int)jAr["uid"],
                        read_state = (short)jAr["read_state"],
                        title = (string)jAr["title"],
                        body = (string)jAr["body"],
                    };

I'd like to restrict the query to to skip the first object in the array but I'm not sure how to do it.

解决方案

var query = from msg in jAr
            where  !(msg is JValue)
            select new
            {
                mid = (int)msg["mid"],
                date = (int)msg["date"],
                outt = (short)msg["out"],
                uid = (int)msg["uid"],
                read_state = (short)msg["read_state"],
                title = (string)msg["title"],
                body = (string)msg["body"],
            };

这篇关于LINQ到JSON选择某些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆