2017-04-21 69 views
0

基本上我有這個JSON和我有基於「用戶標識」獲取基於其價值的一個JSON對象

[ 
    { 
    "Userid": 2, 
    "Id": 99999 
    }, 
    { 
    "Userid": 4, 
    "Id": 55555 
    } 
] 

拿到「身份證」,我需要選擇基礎上的JSON對象「用戶名」。

我該怎麼做?

我用這個線程:

string json = @" 
{ 
    wvw_matches: [ 
     { 
      wvw_match_id: ""1-4"", 
      red_world_id: 1011, 
      blue_world_id: 1003, 
      green_world_id: 1002, 
      start_time: ""2013-09-14T01:00:00Z"", 
      end_time: ""2013-09-21T01:00:00Z"" 
     }, 
     { 
      wvw_match_id: ""1-2"", 
      red_world_id: 1017, 
      blue_world_id: 1021, 
      green_world_id: 1009, 
      start_time: ""2013-09-14T01:00:00Z"", 
      end_time: ""2013-09-21T01:00:00Z"" 
     } 
    ] 
}"; 

string matchIdToFind = "1-2"; 
JObject jo = JObject.Parse(json); 

JObject match = jo["wvw_matches"].Values<JObject>() 
    .Where(m => m["wvw_match_id"].Value<string>() == matchIdToFind) 
    .FirstOrDefault(); 

if (match != null) 
{ 
    foreach (JProperty prop in match.Properties()) 
    { 
     Console.WriteLine(prop.Name + ": " + prop.Value); 
    } 
} 

但由於礦井不具備「wvw_matches」(不知道該怎麼稱呼它),我不知道:Json.Net Select Object based on a value

有解決辦法瞭解我將如何在我的情況下使用它。

+1

'jo.Values ().Where(m => m [「Userid」]。Value ()== matchIdToFind).FirstOrDefault();'? – GSerg

+0

'var str = File.ReadAllText(「E:/User.json」); string json = $「{str}」; JObject jo = JObject.Parse(json); string matchIdToFind =「1」; JObject match = jo.Values ().Where(m => m [「Userid」]。Value ()== matchIdToFind).FirstOrDefault(); 如果(匹配!= NULL){ 的foreach (JProperty丙中match.Properties()){ Console.WriteLine(prop.Name + 「:」 + prop.Value); } }'This throws the error: – datuhaccz

+1

'string json = $「{str}」;'?你採取了哪些步驟來找出File.ReadAllText()返回的內容? –

回答

0

這就是你怎麼做到的。

public class Sample 
{ 
    public string Userid { get; set; } 
    public string Id { get; set; } 
} 


public static void Main(string[] args) 
{ 
     using (var stream = new StreamReader("sample.json")) 
     { 

      var sampleArray = JsonConvert.DeserializeObject<Sample[]>(stream.ReadToEnd()); 

      foreach (var users in sampleArray) 
      { 
       Console.WriteLine("Userid: {0}", users.Userid); 
       Console.WriteLine("Id: {0}", users.Id); 
      } 
     } 

     Console.Read(); 
} 

您需要將您的JSON響應映射到類。而當你有一個數組,你需要

var sampleArray = JsonConvert.DeserializeObject<Sample[]>(stream.ReadToEnd());

希望它爲你工作。

0

假設您沒有大量記錄,請創建一個類來容納JSON所代表的對象。

public class JsonData //name this something relevant to your data. 
{ 
    public int Userid { get; set; } 
    public int Id { get; set; } 

    public static JsonData[] Deserialize(string jsonString) 
    { 
     return JsonConvert.DeserializeObject<JsonData[]>(jsonString); 
    } 
} 

然後只需使用linq表達式來查詢數組。

jsonArray.Where(j => j.Userid == <your value>).Select(j => j.Id); 
0

因爲我寫了你引用的例子,在這裏它被更新爲使用你的JSON。

string json = @" 
[ 
    { 
    ""Userid"": 2, 
    ""Id"": 99999 
    }, 
    { 
    ""Userid"": 4, 
    ""Id"": 55555 
    } 
]"; 

string userIdToFind = "4"; 

JArray ja = JArray.Parse(json); 

JObject match = ja.Children<JObject>() 
    .FirstOrDefault(m => m["Userid"].Value<string>() == userIdToFind); 

if (match != null) 
{ 
    foreach (JProperty prop in match.Properties()) 
    { 
     Console.WriteLine(prop.Name + ": " + prop.Value); 
    } 
}  

小提琴:https://dotnetfiddle.net/vpZpSS

不同的是,你的JSON 陣列,而JSON在原有例子是用含有陣列的屬性的對象。