Newtonsoft.Json之JArray, JObject, JProperty,JValue

JObject staff = new JObject();

            staff.Add(new JProperty("Name", "Jack"));

            staff.Add(new JProperty("Age", 33));

            staff.Add(new JProperty("Department", "Personnel Department"));

            staff.Add(new JProperty("Leader", new JObject(new JProperty("Name", "Tom"), new JProperty("Age", 44), new JProperty("Department", "Personnel Department"))));

            Console.WriteLine(staff.ToString());

 

  

            JArray arr = new JArray();

            arr.Add(new JValue(1));

            arr.Add(new JValue(2));

            arr.Add(new JValue(3));

            Console.WriteLine(arr.ToString());

 

 

 

string json = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }";

Gets the name of the employee

            // will be converted into json JObject

            JObject jObj = JObject.Parse(json);

            // accessed by name or index property, just own the property name, but not all

            JToken ageToken =  jObj["Age"];

            Console.WriteLine(ageToken.ToString());

 

Guess you like

Origin www.cnblogs.com/daimaxuejia/p/11669877.html