2013-07-26 130 views
-2

我的JSON:查詢的JSON數組/ JObject

"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」上並顯示「Value」。我想我需要做類似的事情:

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

回答

2

你的問題得到了一些標記,因爲它有點含糊。

不過,我想我明白你需要什麼...

我覺得最簡單的解析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庫的參考。這是一個NuGet包在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); 
+0

的事情是一個類linq查詢。不知道是否有可能,但是像這樣:ConsentGiven = rss [「RegistrationCase」] [「CustomData」]。Where([「Key」] ==「IsConstantGiven」)[「Value」]。 – stianboe

0

希望這會有所幫助,創建有那麼value屬性做我一直在尋找波紋管

public class Category 
{ 

    public string Value{ get; set; } 

} 

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