あなたの質問は少しあいまいなので、いくつかの点数を下げています。
しかし、私はあなたが必要なものを理解していると思います...
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のナゲットパッケージです
マーティン
編集:これは私が意味する完全に機能するバージョンです。インデックスを使用してさまざまなエントリを見つけることができますが、これは私だけかもしれません.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);