SoFunction
Updated on 2025-03-08

C# Get the value case of a json object with dynamic key

Problem description

If you directly get the element in a json array, you will get the following json

{
 "44": {
  "height": 25,
  "appeared": -70000000,
  "length": 44,
  "order": "saurischia",
  "vanished": -70000000,
  "weight": 135000
 }
}

If this json object is deserialized using C# class, the structure of the entity class is as follows. The class name of the entity class needs to be the same as the json object key before it can be deserialized using json, which causes great inconvenience to the program.

public class 44
{
 public int height { get; set; }
 public int appeared { get; set; }
 public int length { get; set; }
 public string order { get; set; }
 public int vanished { get; set; }
 public int weight { get; set; }
}
public class Root
{
 public 44 44 { get; set; }
}

Solution

The above json object cannot be deserialized using C# because the key is dynamic, but it can be serialized by directly obtaining the value, as follows.

{
 "height":25,
 "appeared":-70000000,
 "length":44,
 "order":"saurischia",
 "vanished":-70000000,
 "weight":135000
}

The above json objects can be converted using our commonly used format.

public class Root
{
 public int height { get; set; }
 public int appeared { get; set; }
 public int length { get; set; }
 public string order { get; set; }
 public int vanished { get; set; }
 public int weight { get; set; }
}

Implement code

Get the value part from the json object of the dynamic key, which can deserialize the string. Please use the following function and pay attention to introducing the class library.

using ;
using ;
using ;
/// <summary>
/// This class is used to handle dynamic key json objects/// </summary>
/// <param name="jObject">json object that needs to be processed</param>/// <returns> values ​​of the first element of the json object</returns>public static string GetJsonValue(string strJson)
{
 string strResult;
 JObject jo = (strJson);
 string[] values = ().Select(item =&gt; ()).ToArray();
 if (values == null)
 {
  strResult = "";
 }
 else
 {
  strResult = values[0];
 }
 return strResult;
}

Supplement: C# Gets the value of the specified KEY in the JSON string

background

Getting JSON format data from the Markdown system API interface, the JSON string is not standardized or it is difficult to obtain the doc_id field (located in the leaf node of the tree, but I don’t know how many branch nodes there are). At this time, I thought of using regular expressions to obtain the value of the specified KEY, so this article was generated.

Applicable scenarios

Don't want to parse the entire JSON string, just want to get the value of one of the KEYs

The object corresponding to the JSON string is difficult to construct, you only need to obtain the values ​​of some of the KEYs.

JSON strings are not standardized, you only need to get the value of the specified KEY (especially some are array objects and some are non-data objects)

Code

/// &lt;summary&gt;
/// Get the value of the specified KEY in the JSON string/// &lt;/summary&gt;
/// &lt;param name="jsonString"&gt;&lt;/param&gt;
/// &lt;param name="key"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public List&lt;String&gt; GetJsonValue(String jsonString, String key)
{
 String pattern = $"\"{key}\":\"(.*?)\\\"";
 MatchCollection matches = (jsonString, pattern, );
 List&lt;string&gt; lst = new List&lt;string&gt;();
 foreach (Match m in matches)
 {
  ([1].Value);
 }
 return lst;
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.