Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 检查NewtonSoft作业对象C中是否存在密钥_Json_Linq_Json.net - Fatal编程技术网

Json 检查NewtonSoft作业对象C中是否存在密钥

Json 检查NewtonSoft作业对象C中是否存在密钥,json,linq,json.net,Json,Linq,Json.net,我有一个JSON对象NewtonSoft.JObject。它包含以下格式的多个条目: { "id": "b65ngx59-2c67-4f5b-9705-8525d65e1b8", "name": "TestSample", "versions": [] }, { "id": "8acd8343-617f-4354-9b29-87a251d2f3e7", "name": "template 2", "versions": [ { "id": "556ng

我有一个JSON对象NewtonSoft.JObject。它包含以下格式的多个条目:

{
  "id": "b65ngx59-2c67-4f5b-9705-8525d65e1b8",
  "name": "TestSample",
  "versions": []
},
{
  "id": "8acd8343-617f-4354-9b29-87a251d2f3e7",
  "name": "template 2",
  "versions": [
    {
      "id": "556ng956-57e1-47d8-9801-9789d47th5a5",
      "template_id": "8acd8343-617f-4354-9b29-87a251d2f3e7",
      "active": 1,
      "name": "1.0",
      "subject": "<%subject%>",
      "updated_at": "2015-07-24 08:32:58"
    }
  ]
}
它给出了所有ID。现在我想给linq添加一个条件,以便在列表中只填充包含值为1的active的模板_ID

所需输出:

[0] - 8acd8343-617f-4354-9b29-87a251d2f3e7
我应该对linq查询做什么更改才能得到这个结果

编辑:完整的代码是

public bool CheckIfTemplateExists(string template)
{
 bool exists = false;

 //web service call to retrieve jsonTemplates
 JObject jObj = JObject.Parse(jsonTemplates);

 List<JToken> templateIdList = jObj.Descendants()
                .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "id")
                .Select(p => ((JProperty)p).Value)
                .ToList();

 if(templateIdList.IndexOf(template) != -1)
  {
   exists = true;
  }
return exists
}
试试这个:


您好,谢谢您的回答。我尝试了您的代码,但出现了错误,无法访问Newtonsoft.Json.Linq.JProperty上的子值。您能否共享整个不起作用的代码片段?我将尝试修复。编译器在到达您所理解的linq查询时给出错误。但是你能分享一下初始化jObj的代码吗?我已经添加了初始化jObj的代码和输入字符串。
[0] - 8acd8343-617f-4354-9b29-87a251d2f3e7
public bool CheckIfTemplateExists(string template)
{
 bool exists = false;

 //web service call to retrieve jsonTemplates
 JObject jObj = JObject.Parse(jsonTemplates);

 List<JToken> templateIdList = jObj.Descendants()
                .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "id")
                .Select(p => ((JProperty)p).Value)
                .ToList();

 if(templateIdList.IndexOf(template) != -1)
  {
   exists = true;
  }
return exists
}
{"templates": 
[{"id":"b65cae59-2c67-4f5b-9705-07465d65e1b8",
"name":"TestSample","versions":[]},
{"id":"8edb8343-617f-4354-9b29-87a251d2f3e7",
"name":"Template1",
"versions":[{"id":"556bb956-57e1-47d8-9801-9388d47cc5a5",
"template_id":"8edb8343-617f-4354-9b29-87a251d2f3e7",
"active":1,
"name":"1.0","subject":"\u003c%subject%\u003e",
"updated_at":"2015-07-24 08:32:58"}]}
List<JToken> templateIdList = jObj["templates"].Children()
            .Where(child => child["versions"] != null 
               && child["versions"].Any(version => version["active"].Value<int>() == 1))
            .Select(x => x["id"]);