SoFunction
Updated on 2025-04-12

C# uses library to implement the extraction function of a certain field value in JSON data

introduction

In C#, libraries (also called) can be used to process JSON data. This library provides a very convenient way to parse and manipulate JSON data. Here are a few examples to show how to extract the value of a field from JSON format text and store it in a string, list, or other generic collection.

1. Extract the value of a single field and store it into a string

Suppose there is the following JSON format text:

{
    "name": "John Doe",
    "age": 30,
    "isStudent": false
}

Want to extractnameThe value of the field is stored in a string.

using ;
using System;

class Program
{
    static void Main()
    {
        // JSON format text        string jsonText = "{\"name\": \"John Doe\", \"age\": 30, \"isStudent\": false}";

        // Parses JSON text to JObject        JObject jsonObject = (jsonText);

        // Extract the value of the "name" field and store it in a string        string name = jsonObject["name"].ToString();

        // Output result        ("Name: " + name);
    }
}

Code comments:

  • (jsonText): parse JSON text into oneJObjectObject.
  • jsonObject["name"]: By field namenameAccess values ​​in JSON objects.
  • .ToString(): Convert the extracted value to a string.

2. Extract the value of the array field and store it in the list

Suppose there is the following JSON format text:

{
    "name": "John Doe",
    "age": 30,
    "hobbies": ["reading", "swimming", "coding"]
}

Want to extracthobbiesThe value of the field is stored in aList<string>middle.

using ;
using System;
using ;

class Program
{
    static void Main()
    {
        // JSON format text        string jsonText = "{\"name\": \"John Doe\", \"age\": 30, \"hobbies\": [\"reading\", \"swimming\", \"coding\"]}";

        // Parses JSON text to JObject        JObject jsonObject = (jsonText);

        // Extract the value of the "hobbies" field and store it in List<string>        List&lt;string&gt; hobbies = jsonObject["hobbies"].ToObject&lt;List&lt;string&gt;&gt;();

        // Output result        ("Hobbies:");
        foreach (var hobby in hobbies)
        {
            (hobby);
        }
    }
}

Code comments:

  • jsonObject["hobbies"]: By field namehobbiesAccess an array in a JSON object.
  • .ToObject<List<string>>(): Convert JSON array toList<string>

3. Extract the value of the nested field and store it in a custom object

Suppose there is the following JSON format text:

{
    "name": "John Doe",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA"
    }
}

Want to extractaddressThe value of the field is stored in a customAddressin object.

using ;
using System;

class Program
{
    static void Main()
    {
        // JSON format text        string jsonText = "{\"name\": \"John Doe\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"Anytown\", \"state\": \"CA\"}}";

        // Parses JSON text to JObject        JObject jsonObject = (jsonText);

        // Extract the value of the "address" field and store it in the Address object        Address address = jsonObject["address"].ToObject&lt;Address&gt;();

        // Output result        ("Address:");
        ("Street: " + );
        ("City: " + );
        ("State: " + );
    }
}

// Custom Address classpublic class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

Code comments:

  • jsonObject["address"]: By field nameaddressAccess nested objects in JSON objects.
  • .ToObject<Address>(): Convert nested JSON objects to customAddressObject.

4. Extract the values ​​of multiple fields and store them in the dictionary

Suppose there is the following JSON format text:

{
    "name": "John Doe",
    "age": 30,
    "isStudent": false
}

Want to extract the values ​​of all fields and store them to oneDictionary<string, object>middle.

using ;
using System;
using ;

class Program
{
    static void Main()
    {
        // JSON format text        string jsonText = "{\"name\": \"John Doe\", \"age\": 30, \"isStudent\": false}";

        // Parses JSON text to JObject        JObject jsonObject = (jsonText);

        // Create a dictionary to store the values ​​of all fields        Dictionary&lt;string, object&gt; data = new Dictionary&lt;string, object&gt;();

        // Iterate through all fields in the JSON object        foreach (var property in ())
        {
            data[] = &lt;object&gt;();
        }

        // Output result        foreach (var item in data)
        {
            ($"{}: {}");
        }
    }
}

Code comments:

  • (): Get all fields in the JSON object.
  • <object>(): Convert the value of the field toobjectType and store in a dictionary.

Summarize

Through the above example, you can see how to use itThe library extracts field values ​​in JSON format text in C# and stores them into strings, lists, custom objects, or dictionaries. These methods can be flexibly applied to various JSON data processing scenarios.

This is the article about using C# libraries to implement the extraction function of a certain field value in JSON data. For more related C# libraries to extract JSON fields, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!