C# json数组/JObject中的查询

C# json数组/JObject中的查询,c#,json,linq,C#,Json,Linq,我的json: "CustomData": [ { "Key": "RegistrationWrx", "Value": "Wrx45687", "Id": 462, }, { "Key": "IsConsentGiven", "Value": "True", "Id": 463, }, 我用它来获得一些值: string fetchResult = JsonConvert.SerializeObject(sidebar, For

我的json:

"CustomData": [
  {
    "Key": "RegistrationWrx",
    "Value": "Wrx45687",
    "Id": 462,
  },
  {
    "Key": "IsConsentGiven",
    "Value": "True",
    "Id": 463,
  },
我用它来获得一些值:

string fetchResult = JsonConvert.SerializeObject(sidebar, Formatting.Indented);
JObject rss = JObject.Parse(fetchResult);

ConsentGiven = rss["RegistrationCase"]["CustomData"][1]["Value"].Value<string>(),
string fetchResult=JsonConvert.serialized对象(侧栏,格式化.缩进);
JObject rss=JObject.Parse(fetchResult);
Approvisive=rss[“RegistrationCase”][“CustomData”][1][“Value”].Value(),
但我想检查“键”,例如“CustomData”上的键,并显示“Value”。我想我需要做一些事情,比如:

ConsentGiven = rss["RegistrationCase"]["CustomData"].Where(["Key"]=="IsConstantGiven")["Value"].Value<string>(),
approvisive=rss[“RegistrationCase”][“CustomData”]。其中([“Key”]==“IsConstantGiven”)[“Value”]。Value(),

你的问题有点含糊不清,所以被打了两分

但是,我想我知道你需要什么

我发现解析json内容最简单的方法是首先转换它

因此,创建与传入json匹配的类:

public class CustomData{
    public string Key {get;set;}
    public string Value {get;set}
    public int? ID {get;set;}
}
然后,用什么方法读取json,实例化并转换该类型的对象

public CustomData ConvertCustomDataJson(string jsonString)
{
List<CustomData> customData = JsonConvert.DeserializeObject<List<CustomData>>(jsonString);
}
此外,您还需要对NewtonSoft json库的引用。这是VS 2012中的一个nuget包

马汀

编辑:这是我的意思的一个完全有效的版本,你可以通过使用索引找到不同的条目,但是,这可能就是我,我感到紧张,因为我永远不知道json内容是否会改变

序列化对象意味着它应该处理json或其他数据的大部分更改,加上强类型的好处只是使其更易于读取

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqFun
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set Data
            string jsonString = @"[
                                      {
                                        ""Key"": ""RegistrationWrx"",
                                        ""Value"": ""Wrx45687"",
                                        ""Id"": 462,
                                      },
                                      {
                                        ""Key"": ""IsConsentGiven"",
                                        ""Value"": ""True"",
                                        ""Id"": 463,
                                      }
                                   ]";

            //Create a list of CustomData entries to look through.
            List<CustomData> customData = JsonConvert.DeserializeObject<List<CustomData>>(jsonString);


            //Create an object for the is consent given block of data

            CustomData IsConsentGiven = customData.FirstOrDefault(x => x.Key == "IsConsentGiven");

            //check the linq query resulted in an object
            if (IsConsentGiven != null)
            {
                Console.WriteLine(IsConsentGiven.Value);
            }

            Console.ReadLine();
        }
    }

     public class CustomData{
         public string Key { get; set; }
        public string Value { get; set; }
        public int? ID { get; set; }
    }
}

希望这会有所帮助,创建一个具有value属性的类,然后执行以下操作

  public class Category
{

   public string Value{ get; set; }

 }

 var categories = JsonConvert.DeserializeObject<List<Category>>(json)
公共类类别
{
公共字符串值{get;set;}
}
var categories=JsonConvert.DeserializeObject(json)

我要找的是linq查询。不确定是否可能,但如下所示:ApprovementGiven=rss[“RegistrationCase”][“CustomData”]。其中([“Key”]=“IsConstantGiven”)[“Value”]。Value(),
bool value = Convert.ToBoolean(customData.FirstOrDefault(x => x.Key == "IsConsentGiven").Value);
  public class Category
{

   public string Value{ get; set; }

 }

 var categories = JsonConvert.DeserializeObject<List<Category>>(json)