json数组/JObject中的查询



我的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>(),

但我想检查"密钥",例如在"CustomData"上,并显示"值"。我想我需要做一些类似的事情:

ConsentGiven = rss["RegistrationCase"]["CustomData"].Where(["Key"]=="IsConstantGiven")["Value"].Value<string>(),

您的问题因为有点模糊而被打了几分。

但是,我想我理解你需要什么。。。

我发现解析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);
}

然后你可以使用你的对象轻松地循环它们,存储它们,随意使用它们。

我很快就解决了这个问题,所以它可能并不完美。

Linq查询以查找值

bool value = Convert.ToBool(customData.FirstOrDefault(x=> x.Key == "IsConsentGiven").Value);

此外,您还需要对NewtonSoft json库的引用。这是VS 2012 中的一个nuget包

Martyn

编辑:这是我的意思的一个完整的工作版本,你可以通过使用索引找到不同的条目,但是,这可能只是我,我很紧张,因为我永远不知道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; }
    }
}

你可以直接提取IsConsentGiven的值,但如果你必须将其包含在try块中以防数据丢失,我更喜欢自己检查它。直接将其拉出的linq是:

bool value = Convert.ToBoolean(customData.FirstOrDefault(x => x.Key == "IsConsentGiven").Value);

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

  public class Category
{
   public string Value{ get; set; }
 }
 var categories = JsonConvert.DeserializeObject<List<Category>>(json)