C # Foundation, Joject Jarray constructs a JSON object

Source: Internet
Author: User

The first two days to use the JSON object, a long time no use jobject. The usage has been forgotten. After a half-day to smattering. Temporarily put the half-solution to the record, lest forget later, if can help other people that also good

The first two classes to use. Jobject and Jarray need to introduce libraries: Newtonsoft.Json.dll

The demo I use is a basic MVC project.

situation One : Returns a basic JSON object:

Background code:

         Public jobject getobj ()        {            =new  jobject ();            Obj. ADD ("Name","Ray");             return obj;        }

The following results are available through Chrome access:

Scenario Two : The production environment will certainly not be so simple, there must be more than one key value pair, I just got stuck here.

Returns a slightly more complex JSON string:

Background code:

         Public jobject getobj ()        {            =new  jobject ();            Obj. ADD ("Name","Ray");            Obj. ADD ("Gender","Male");             return obj;        }

The following results are available through Chrome access:

situation three : still can not meet the actual demand here, perhaps we need sub-objects, such as adding a contact, contact telephone and QQ

Background code:

         Publicjobject getobj () {Jobject obj=NewJobject (); Obj. ADD ("Name","Ray"); Obj. ADD ("Gender","Male"); //Add a contact formJobject contact=NewJobject (); Contact. ADD ("Phone","151XXXXXXXX"); Contact. ADD ("QQ","123456789"); Obj. ADD (" Contact", contact); returnobj; }

The following results are available through Chrome access:

actually later vs's powerful plugin resharper hint I can use a more concise approach when defining Jobject : Please refer to this definition method, not one add:

New jobject {"Name""Tom"}, {"Gender " " Male "}};

All right, here we go. The use of Jobject is already going on. However, in practice, some arrays are included in the JSON object. We're going to use the Jarray at this time.

situation one : the most basic Jarray

Background code:

         PublicJarray Getjarray () {Jarray array=NewJarray (); Jobject Tom=NewJobject {{"Name","Tom"}, {"Gender","Male"}}; Jobject Jack .=Newjobject{{"Name","Ray"},{"Gender","Female"}}; Array.            ADD (Tom); Array.            ADD (Jack); returnArray; }

The following results are available through Chrome access:

situation Two: in fact, this syntax is very few, usually still appear nested situation, see below

Background code:

         PublicJarray GetNestArray2 () {//Create a new two Jobject objectJobject Kevin =NewJobject (); Kevin. ADD ("name","Kevin"); Kevin. ADD (" Age", -); Jobject Jack .=NewJobject (); Jack. ADD ("Name","Jack"); Jack. ADD (" Age", -); //Create a new Jarray objectJarray OBJJ =NewJarray (); OBJJ.            ADD (Kevin); OBJJ.            ADD (Jack); //Create a new outermost Jarray objectJarray rearray=NewJarray (); //an Jarray array is added in a convenient way. Refer to the method above new JobjectRearray.add (NewJarray {NewJobject {{"Name","Lucy"}, {" Age", -} },NewJobject {{"Name","Lily"}, {" Age", - } } }); //Add first Jarray objectRearray.add (OBJJ); returnRearray; }
View Code

It can be understood that a [] necessarily corresponds to a Jarray object.

The following results are available through Chrome access:

situation Three: in the actual situation Jarray and jobject mix. Take a look at the following code:

Background code:

         PublicJarray Getnestarray () {jobject Kevin=Newjobject{{"name","Kevin"},{" Age", -}}; Jobject Jack .=NewJobject {{"Name","Jack"}, {" Age", -}}; Jobject Obj4=NewJobject (); Jarray Childarray=NewJarray {kevin, Jack}; Obj4. ADD ("Array", Childarray); Jarray OBJJ=NewJarray {Kevin, Jack, Obj4}; returnOBJJ; }
View Code

Access to chrome will result in the following (in fact, an array of content stored in the same format should be the same, and this example is only used for demonstration purposes. Production environments consider other data formats):

The method returns a Jarray object

Situation four: Return is a Jobject object, common cases

Background code (business logic is to get information about a student, including two course scores for a student):

         Publicjobject Getstudentinfo () {//Create a new Jobject object add two key-value pairsJobject studentinfo=Newjobject{{"name","Kevin"},{"Gender","Male"}}; //creates a new Jarray object that can be interpreted as an array object of value position in a JSON object.Jarray courses =NewJarray {NewJobject {{"Coursename","Math"}, {"score", the} },NewJobject {{"Coursename","中文版"}, {" Age", the } } }; Studentinfo.add ("Courses", courses); returnStudentinfo; }

The following results are available through Chrome access:

Okay, here you are ready to deal with the general information.

Complete! ^_^

C # Foundation, Joject Jarray constructs a JSON object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.