无法访问Newtonsoft.Json.Linq.JProperty上的子值-使用LinQ检查JObject时发生错误 [英] Cannot access child value on Newtonsoft.Json.Linq.JProperty -Error happens while checking JObject using LinQ

查看:516
本文介绍了无法访问Newtonsoft.Json.Linq.JProperty上的子值-使用LinQ检查JObject时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Json数据的JObject对象.我需要收集所有具有"state": true的KeyValuePair.在读取值之前,我想确保JObject具有至少一个KeyValuePair,而JToken(值)具有"state": true.
以下是我的JSON:

I have a JObject object which has Json data. I need to collect all KeyValuePairs of whichever has "state": true. Before I read the value, I want to make sure that the JObject has at least one KeyValuePairs with JToken (Value) has "state": true.
Below is my JSON:

{  
  "AAA": {
    "state": false,
    "version": "1.1.14202.0",
    "result": null,
    "update": "20171018"
  },
  "BBB": {
    "state": true,
    "version": "3.10.1.18987",
    "result": null,
    "update": "20171018"
  },
  "CCC": {
    "state": true,
    "version": "1.1.1.2",
    "result": null,
    "update": "20171018"
  }
}

下面是我正在检查的代码,它引发了一个异常,提示Cannot access child value on Newtonsoft.Json.Linq.JProperty :

And the below is the code currently I'm checking with, which is throwing an exception saying Cannot access child value on Newtonsoft.Json.Linq.JProperty:

JObject jsonData = //JSON data;
List<JToken> tokens = jsonData .Children().ToList();
if (tokens.Any(each => each["state"].ToString().ToLower().Contains("true")))
{
  List<JToken> tokensWithStateTrue = tokens.Where(each => each["state"].ToString().ToLower().Contains("true")).ToList();
}

请帮助我,并纠正LinQ语句,使其仅读取具有 state true 的JToken.

Please help me and correct the LinQ statement to read only JTokens with state as true.

推荐答案

这对我有用,似乎您错过了对Children()的额外调用,以访问所需的属性.

This worked for me, looks like you're missing an extra call to Children() to access the properties you need.

//parse JSON and grab it's children. 
var jsonData = JObject.Parse(json).Children();

List<JToken> tokens = jsonData .Children().ToList(); 

List<JToken> tokens = jsonData .Children().Children().ToList();
if (tokens.Any(each => each["state"].ToString().ToLower().Contains("true")))
{
    List<JToken> tokensWithStateTrue = tokens.Where(each => each["state"].ToString().ToLower().Contains("true")).ToList();
}

或者,您可以执行此操作.下面的代码将返回一个字典,其中仅包含具有真实值的状态.否则,如果没有真值,它将返回一个空字典.

Alternatively you could do this. The code below will return a dictionary with only your states with true values. Otherwise, it will return an empty dictionary if you have no true values.

var dictionaryTokensWithTrueValues = jsonData.Children()
.Select(u => u as JProperty)
.Where(v => v.Value["state"].ToString().ToLower().Contains("true"))
.ToDictionary(k => k.Name, v => v.Value);

//check if you have any true values
if (dictionaryTokensWithTrueValues.Count() > 0)
{
     //do something with true states here
     var accessBBB = dictionaryTokensWithTrueValues["BBB"]; //{{"state": true,"version": "3.10.1.18987","result": null,"update": "20171018"}}
}
else
{ 
     //no true states. Do something else
}

这篇关于无法访问Newtonsoft.Json.Linq.JProperty上的子值-使用LinQ检查JObject时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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