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 extractname
The 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 oneJObject
Object. -
jsonObject["name"]
: By field namename
Access 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 extracthobbies
The 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<string> hobbies = jsonObject["hobbies"].ToObject<List<string>>(); // Output result ("Hobbies:"); foreach (var hobby in hobbies) { (hobby); } } }
Code comments:
-
jsonObject["hobbies"]
: By field namehobbies
Access 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 extractaddress
The value of the field is stored in a customAddress
in 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<Address>(); // 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 nameaddress
Access nested objects in JSON objects. -
.ToObject<Address>()
: Convert nested JSON objects to customAddress
Object.
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<string, object> data = new Dictionary<string, object>(); // Iterate through all fields in the JSON object foreach (var property in ()) { data[] = <object>(); } // Output result foreach (var item in data) { ($"{}: {}"); } } }
Code comments:
-
()
: Get all fields in the JSON object. -
<object>()
: Convert the value of the field toobject
Type 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!