Use of JSON (Jobect,jsondata,jarray)

Source: Internet
Author: User

    1. The difference between Jobect and Jsondata

Jsondata is a simplified version of Jobect

2. Bug that the conversion process is easy to make

Jsondata: cannot be converted when there is null inside

Solution: Jsondata TEAMJD = Jsonmapper.toobject (Finddoctor ("0", OrgID).     Replace (": null", ": \" \ ")"); Replace NULL in Jsondata with ""

Jobect: Conversion to Jobect failed

Solution: The reason for the failure is that the conversion object is an array to be converted to Jarray Jarray jarray=jarray.parse (str);

Jobect:jarray[0] cannot be converted directly to Jobect

Solution: Jobject jobject = jarray[0] as jobject;

Jobect: Array cannot be evaluated with count when Jobect object exists inside

Solution:

foreach (jtoken field in jobject["Data" ["doctors"])
{

if (field["name"]. ToString ()). Equals (Docname.trim ()))
{
Doctorid = field["id"]. ToString ();
Break
}
}

3. Determine that the JSON value is empty

JOBECT:JB. ToString (). Equals ("") returns Jsonobect if it is not empty

Jsondata:jd. Count==0

Conversion of 4.json objects

Jsondata: When an array exists inside

json["userfamilyhistory"] = new Jsondata ();

json["Userfamilyhistory"]. Setjsontype (Jsontype.array);
json["Userfamilyhistory"]. ADD (userfamilyhistory);

Jobect: When an array exists inside

list<jobject> listhistory = new list<jobject> ();

Object other = new Jobject ();

...............

Listhistory.add (other);

Jobject.add (New Jproperty ("History", listhistory));

Note: Jsonconvert.serializeobject () can convert a class, DataTable directly into a JSON string

5. Tips for JSON applications

CREATE table from JSON private void CreateTable (Jarray jarray,string tabname) {jobject jobject = jarray[0] as jobject;// Configure the Contract table DataTable SIGNTB = new DataTable (tabname); foreach (var item in Jobject) SIGNTB.COLUMNS.ADD (item. Key), foreach (Jobject obj in Jarray) {DataRow NewRow = Signtb.newrow (); foreach (var item in obj) {newrow[item. Key] = Item. Value;} SIGNTB.ROWS.ADD (NewRow);} Commonrse.ds[tabname] = Signtb.copy ();}

  

Use of JSON (Jobect,jsondata,jarray)

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.