考虑以下JSON格式:
<code class="language-json">{ "displayFieldName": "OBJECT_NAME", "fieldAliases": { "OBJECT_NAME": "OBJECT_NAME", "OBJECT_TYPE": "OBJECT_TYPE" }, "positionType": "point", "reference": { "id": 1111 }, "objects": [ { "attributes": { "OBJECT_NAME": "test name", "OBJECT_TYPE": "test type" }, "position": { "x": 5, "y": 7 } } ] }</code>
要仅提取“objects”数组中的数据,您可以使用Json.net将JSON反序列化为.NET对象:
<code class="language-csharp">Foo foo = JsonConvert.DeserializeObject<Foo>(json);</code>
定义以下类以匹配JSON结构:
<code class="language-csharp">public class NameTypePair { public string OBJECT_NAME { get; set; } public string OBJECT_TYPE { get; set; } } public class Position { public int x { get; set; } public int y { get; set; } } public class SubObject { public NameTypePair attributes { get; set; } public Position position { get; set; } } public class Foo { public List<SubObject> objects { get; set; } }</code>
反序列化后,您可以使用foo.objects
属性访问“objects”数组中的数据。
以上是如何使用 Json.net 从 JSON 数组中提取数据?的详细内容。更多信息请关注PHP中文网其他相关文章!