Using json.net to analyze json
json.net is a popular library to process JSON data in the .NET environment. It provides a convenient way to analyze the JSON string and serialize it into a C#object.
Analysis of JSON
To analyze JSON as an object, you can use the method, where T is the type of object you want to create. For the JSON provided, you can create the following categories:
JsonConvert.DeserializeObject<T>
After creating these classes, you can analyze JSON as FOO object:
<code class="language-csharp">public class DisplayFieldName { public string OBJECT_NAME { get; set; } } public class NameTypePair { public string Name { get; set; } public string Type { get; set; } } public class FieldAliases { public NameTypePair OBJECT_NAME { get; set; } public NameTypePair OBJECT_TYPE { get; set; } } public class PositionType { public string Type { get; set; } } public class Reference { public int Id { get; set; } } public class Attributes { 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 ObjectItem { public Attributes Attributes { get; set; } public Position Position { get; set; } } public class Foo { public DisplayFieldName DisplayFieldName { get; set; } public FieldAliases FieldAliases { get; set; } public PositionType PositionType { get; set; } public Reference Reference { get; set; } public List<ObjectItem> Objects { get; set; } }</code>
Access the nested attribute
<code class="language-csharp">string 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 } } ] }"; Foo foo = JsonConvert.DeserializeObject<Foo>(json);</code>
To access the data in the FOO object, you can use attributes and indexes. For example, to obtain the "test name" value from the Objects array, you can use:
The above is the detailed content of How to Parse Nested JSON Data into C# Objects using Json.NET?. For more information, please follow other related articles on the PHP Chinese website!