Home >Backend Development >C++ >How Can I Efficiently Parse JSON with Json.net to Extract Specific Object Attributes and Positions?

How Can I Efficiently Parse JSON with Json.net to Extract Specific Object Attributes and Positions?

DDD
DDDOriginal
2025-01-24 15:06:10393browse

How Can I Efficiently Parse JSON with Json.net to Extract Specific Object Attributes and Positions?

Use Json.NET to efficiently parse JSON data and extract object attributes

Json.NET provides multiple methods to parse JSON data. One way is to use the JsonTextReader class, which allows sequential reading of JSON data. However, for the specific JSON structure mentioned in the question, a combination of serialization and LINQ is more efficient.

Using serialization and LINQ to parse JSON, we can define a series of classes corresponding to the JSON structure:

<code class="language-csharp">class NameTypePair
{
    public string OBJECT_NAME { get; set; }
    public string OBJECT_TYPE { get; set; }
}

class Reference
{
    public int id { get; set; }
}

class Position
{
    public int x { get; set; }
    public int y { get; set; }
}

class SubObject
{
    public NameTypePair attributes { get; set; }
    public Position position { get; set; }
}

class Foo
{
    public string displayFieldName { get; set; }
    public NameTypePair fieldAliases { get; set; }
    public string positionType { get; set; } // 假设positionType是字符串类型
    public Reference reference { get; set; }
    public List<SubObject> objects { get; set; }
}</code>

After defining these classes, we can use Json.NET’s JsonConvert class to deserialize JSON data:

<code class="language-csharp">Foo foo = JsonConvert.DeserializeObject<Foo>(jsonString);</code>

where jsonString is JSON data represented as a string.

After deserializing the JSON data into the Foo object, we can iterate over the foo.objects array to access each object’s attribute (OBJECT_TYPE) and position (x and y) data:

<code class="language-csharp">foreach (var obj in foo.objects)
{
    Console.WriteLine($"对象类型: {obj.attributes.OBJECT_TYPE}");
    Console.WriteLine($"位置: ({obj.position.x}, {obj.position.y})");
}</code>

This method takes advantage of the powerful deserialization capabilities of Json.NET and the concise syntax of LINQ, making the code more readable, easier to maintain, and more efficient when processing large JSON data. Note that the type of positionType is adjusted based on the actual JSON structure, assuming a string type. If the actual types are different, please modify the code according to the actual situation.

The above is the detailed content of How Can I Efficiently Parse JSON with Json.net to Extract Specific Object Attributes and Positions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn