JSON.NET - 获取嵌套值

时间:2017-07-17 07:54:19

标签: json.net

我有一个像这样的JSON:

  {
    "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'

1 个答案:

答案 0 :(得分: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");