Recently dynamically parse json in the project, but there are many ways to resolve json, how to resolve the problem is that we need to consider? For example Newtonsoft.Json.Linq provided under JToken, JObject the like, JsonConvert available under Newtonsoft.Json.
JObject | For operating the objects json |
JArray | For operating the array json |
JValue | An array of values |
JProperty | Denotes object attribute to "key / value" form |
JToken | Linq used to store the results of queries to the Json |
We mainly describes the different analytical methods applicable scenarios and usage today:
1、JObject
JObject operation target, if used to resolve other types will be reported json 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", "撩妹")))); Console.WriteLine(jObject.ToString());
结果:
{
"name": "caixukun",
"skill": {
"id": 1,
"name": "撩妹"
}
}
2, JArray
JArray for operating the array, if used to resolve other types will be reported json 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());
结果:
[
1,
2,
3
]
3, JToken
string json = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }"; // 获取员工名称 JObject jObject = JObject.Parse(json); var name = jObject.Value<string>("Name"); Console.WriteLine(name); // 获取员工年龄 JToken jToken = jObject.SelectToken("Age"); Console.WriteLine(jToken.ToString()); "Colleagues" JToken jToken1 = jobject [get Colleagues information//]; Console.WriteLine(jToken1.ToString()); Console.WriteLine("============================="); // 获取员工同事的所有姓名 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 ()); // modify colleagues Tome age jToken1 [ 0 ] [ " Age " ] = 45 ; Console. the WriteLine (jObject.ToString ()); Console.WriteLine ( " ============================= " ); // Abel departure the jobject [ " colleagues " ] [ 1 ] .remove (); Console.WriteLine (jObject.ToString ()); // remove Jack's colleagues jObject.Remove ( " Colleagues " ); Console.WriteLine (jObject.ToString ()); Console.WriteLine ( " ========================= ==== " ); // Jack department lacks information jobject [ " Age " ] .Parent.AddAfterSelf ( new new JProperty ( " department " , " president's Office " )); // to a new employee Jerry jObject Linda = new new JObject ( new new JProperty ( " the Name " , " Linda " ),new JProperty("Age", "23")); jObject.Add(new JProperty("Colleagues", new JArray() { linda })); Console.WriteLine(jObject.ToString());
Reference: https://www.cnblogs.com/klsw/p/5904573.html