1, Jobject: Basic JSON Object
<summary>
///Gets The J object.
</summary>
///<returns></returns> public
jobject getjobject ()
{
var obj = new jobject {{' Name ', ' Mark '}};
return obj;
}
2. Jobject: Nested subforms (jobject jobject)
<summary>
///Gets The J object.
</summary>
///<returns></returns> public
jobject getjobject ()
{
var obj = New Jobject {{' Name ', ' Mark '}, {' Age ', 8}};
var info = new Jobject {{"Phone", "132****7777"}, {"Gender", "Man"}};
Obj. ADD ("info", info);
return obj;
}
3. Jarray: Arrays in basic JSON objects
<summary>
///Gets the J array.
</summary>
///<returns></returns> public
jarray Getjarray ()
{
var jarray = new Jarray ();
var mark = new Jobject {{"Name", "Mark"}, {"Age", 8}};
var jack = new Jobject {{"Name", "Jack"}, {"Age", 9}};
Jarray. ADD (Mark);
Jarray. ADD (Jack);
return jarray;
}
4, Jarray: Multiple JSON object array
<summary>
///Gets the J array.
</summary>
///<returns></returns> public
jobject Getjarray ()
{
var obj = new Jobject ();
var student = new Jarray
{
new Jobject {{"name", "Mark"}, {"Age", 8}},
new Jobject {{"name", "Jack"}, {"Age", 9}}}
;
var results = new Jarray
{
new Jobject {{"Subject", "language"}, {"Score",}},
new Jobject {{"Subject", "Math "}, {" Score "
;
Obj. ADD ("Student", Student);
Obj. ADD ("Results", Results);
return obj;
}
5, Jarray:json array nested arrays (one student corresponding to multiple course scores)
<summary>
///Gets the results.
</summary>
///<returns></returns> public
jobject getresults ()
{
var mark = new Jobject {{"Name", "Mark"}, {"Age", "8"}};
var results = new Jarray
{
new Jobject {{"Subject", "language"}, {"Score",}},
new Jobject {{"Subject", "math" }, {"Score"
;
Mark. ADD ("Results", Results);
return mark;
//--------------------------------------------------------------------------
LINQ to JSON is used to manipulate JSON objects. Can be used to quickly query, modify, and create JSON objects. When the JSON object content is more complex and we need only a small fraction of it, consider using LINQ to JSON to read and modify part of the data instead of deserializing all. two. Creating JSON arrays and objects
Before you make LINQ to JSON, you first need to understand the classes used to manipulate LINQ to JSON.
Class name |
Description |
Jobject |
For manipulating a JSON object |
Jarray |
Manipulating JSON arrays |
Jvalue |
Represents a value in an array |
Jproperty |
Represents an attribute in an object, in the form of "Key/value" |
Jtoken |
Results for storing LINQ to JSON queries |
1. Create a JSON object
Jobject staff = new Jobject ();
Staff. ADD (New Jproperty ("Name", "Jack"));
Staff. ADD (New Jproperty ("Age");
Staff. ADD (New Jproperty ("Department", "Personnel Department"));
Staff. ADD (New Jproperty ("Leader", New Jobject ("Name", "Tom"), New Jproperty ("Age"), New Jproperty (" Department "," Personnel Department ")));
Console.WriteLine (staff. ToString ());
Results:
In addition, you can also use a way to get jobject.jarray similar.
Method |
Description |
Jobject.parse (String json)
|
JSON contains a string of JSON objects, returned as a Jobject object |
Jobject.fromobject (Object o)
|
O for the object to be transformed, returns a Jobject object |
Jobject.load (Jsonreader Reader)
|
Reader contains the contents of the JSON object, returning a Jobject object |
2. Create a JSON array
Jarray arr = new Jarray ();
Arr. ADD (New Jvalue (1));
Arr. ADD (New Jvalue (2));
Arr. ADD (New Jvalue (3));
Console.WriteLine (arr. ToString ());
Results:
three. Using LINQ to JSON
1. Query
First prepare the JSON string, a JSON that contains basic employee information
String json = "{\" name\ ": \" Jack\ ", \" age\ ":" Colleagues\ ": [{\" name\ ": \" tom\ ", \" age\ ": 44},{\" name\ ": \" abel\ ", \" age\ ": 29}]}";
① get the name of the employee
Converts JSON to jobject
jobject jobj = Jobject.parse (JSON);
Accessed through a property name or index, only its own property name, not all
jtoken Agetoken = jobj["age"];
Console.WriteLine (Agetoken.tostring ());
Results:
② get all the names of the employee's colleagues
Converts JSON to jobject
jobject jobj = Jobject.parse (JSON);
var names=from staff in jobj["colleagues"]. Children ()
Select (String) staff["Name";
foreach (var name in names)
Console.WriteLine (name);
"Children ()" Can return objects in all arrays
Results: