2017-07-17 88 views
0

我有一個JSON是這樣的:JSON.NET - 獲取嵌套值

{ 
    "key": "Target", 
    "value": { 
     "__type": "Entity:http://schemas.microsoft.com/xrm/2011/Contracts", 
     "Attributes": [ 
     { 
      "key": "prioritycode", 
      "value": { 
      "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts", 
      "Value": 1 
      } 
     }, 
     { 
      "key": "completeinternalreview", 
      "value": false 
     }, 
     { 
      "key": "stepname", 
      "value": "10-Lead" 
     }, 
     { 
      "key": "createdby", 
      "value": { 
      "__type": "EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts", 
      "Id": "ca2ead0c-8786-e511-80f9-3863bb347b18", 
      "KeyAttributes": [], 
      "LogicalName": "systemuser", 
      "Name": null, 
      "RowVersion": null 
      } 
     } 
     ] 
    } 
    } 

如何通過搜索關鍵字的價值托克的鍵/值?

例如,我想要得到的鍵值對「completeinternalreview」

+0

反序列化JSON字符串後,您可以使用'value.Attributes'對象執行簡單的LINQ查詢。 – danielspaniol

回答

0

假設你有一個C#類這樣的代表,從您的JSON屬性對象:

public class MyValue 
{ 
    [JsonProperty("Attributes")] 
    public List<KeyValuePair<string, object>> Attributes { get; set; } 
} 

你可以簡單地反序列化字符串:

var result = JsonConvert.DeserializeObject<KeyValuePair<string, MyValue>>(jsonString); 

,然後找到正確的鍵 - 值對有:

var kvp = result.Value.Attributes.Find(a => a.Value == "completeinternalreview"); 
+0

非常好,正是我需要的! – Stanza