Dynamic Analysis of Json

Posted by XxDeadmanxX on Sun, 06 Oct 2019 14:33:25 +0200

Recently, we need to dynamically parse JSON in our project, but there are many ways to parse json. How to reasonably parse JSON is the problem we need to consider? such as Newtonsoft.Json.Linq JToken, JObject and so on are provided below, and JsonConvert is provided under Newtonsoft.Json.

JObject  Used to manipulate json objects
JArray For manipulating json arrays
JValue Values representing arrays
JProperty  Represents attributes in objects in the form of "key/value"
JToken Used to store the results of Linq to Json queries

 

 

 

 

 

 

Today we will focus on the scenarios and usage of different parsing methods:

1,JObject

JObject is used to manipulate objects. If it is used to parse other types of json, it will report Newtonsoft.Json.JsonReaderException.  

JObject jObject = new JObject();
jObject.Add(new JProperty("name", "caixukun"));
jObject.Add(new JProperty("skill", new JObject(new JProperty("id", 1), new JProperty("name", "Pick up younger sister"))));
Console.WriteLine(jObject.ToString());
Result:
{
"name": "caixukun",
  "skill": {
    "id": 1,
    "name": "Pick up younger sister"
  }

 2,JArray

JArray is used to manipulate arrays. If it is used to parse other types of json, it will report Newtonsoft.Json.JsonReaderException.  

JArray arr = new JArray();
arr.Add(new JValue(1));
arr.Add(new JValue(2));
arr.Add(new JValue(3));
Console.WriteLine(arr.ToString());
Result:
[
 1,
 2,
 3
]

3,JToken 

string json = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }";
// Get the employee name
JObject jObject = JObject.Parse(json);
var name = jObject.Value<string>("Name");
Console.WriteLine(name);
// Obtaining Employee Age
JToken jToken = jObject.SelectToken("Age");
Console.WriteLine(jToken.ToString());
// Access to colleague information
JToken jToken1 = jObject["Colleagues"];
Console.WriteLine(jToken1.ToString());
Console.WriteLine("=============================");
// Get all the names of staff colleagues.
var names = from staff in jToken1.Children()
            select (string)staff["Name"];
// var names = jObject.SelectToken("Colleagues").Select(p => p["Name"]).ToList();
foreach (var item in names)
{
    Console.WriteLine(item);
}
Console.WriteLine("=============================");
// modify Jack Age
jObject["Age"] = 99;
Console.WriteLine(jObject.ToString());
// Revise colleagues Tome Age
jToken1[0]["Age"] = 45;
Console.WriteLine(jObject.ToString());
Console.WriteLine("=============================");
// Abel Quit
jObject["Colleagues"][1].Remove();
Console.WriteLine(jObject.ToString());
// remove Jack Colleague
jObject.Remove("Colleagues");
Console.WriteLine(jObject.ToString());
Console.WriteLine("=============================");
// Jack Lack of sectoral information
jObject["Age"].Parent.AddAfterSelf(new JProperty("Department", "CEO Office"));
// Here comes a new employee Jerry
JObject linda = new JObject(new JProperty("Name", "Linda"), new JProperty("Age", "23"));
jObject.Add(new JProperty("Colleagues", new JArray() { linda }));
Console.WriteLine(jObject.ToString());

Reference resources: https://www.cnblogs.com/klsw/p/5904573.html

Topics: C# JSON