The difference between Net.sf.json.JSONObject and Org.json.JSONObject

Source: Internet
Author: User

http://my.oschina.net/wangwu91/blog/340721

The difference between Net.sf.json.JSONObject and Org.json.JSONObject.

First, create a JSON object

String str = "{\" code\ ": \" 0000\ ", \" msg\ ": {\" availablebalance\ ": 31503079.02}}

Org.json.JSONObject:

Jsonobject json = new Jsonobject (str);

Net.sf.json.JSONObject:

Jsonobject JSON = Jsonobject.fromobject (str); Net.sf.json.jsonobject no new Jsonobject (String) Construction method

Second, parsing JSON

The first is the direct use of JSON objects. GetXXX (); method gets

Net.sf.json.JSONObject: No strict requirements gets the type of field that is the same as the type of getxxx ()

Org.json.JSONObject: The type of field to get must be the same as the type of getxxx ()

e.g.

Jsonobject msgobj = Json.getjsonobject ("msg");

String availablebalance = msgobj.getstring ("availablebalance");

If the org.json.JSONObject will be error, you can msgobj.getdouble ("Availablebalance"), and will not lose precision While Net.sf.json.JSONObject is correct, the accuracy is lost if string str = "{\" code\ ": \" 0000\ ", \" msg\ ": {\" availablebalance\ ": \" 31503079.02 \"}}";

There is no loss of precision.

The second JSON object transforms the entity object directly

public class Balancedto {
Private String availablebalance;
Public String getavailablebalance () {
return availablebalance;
}
public void Setavailablebalance (String availablebalance) {
This.availablebalance = availablebalance;
}
Public String toString () {

Return "Availablebalance" +availablebalance;
}


}
Org.json.JSONObject:

Balancedto alancedto = (balancedto) jsonobject. Stringtovalue (Msgobj);

This sentence compiles through, but the run will error because the type of availablebalance in the Balancedto class does not agree with the "availablebalance" type in JSON

Net.sf.json.JSONObject:

String msg = json.getstring ("msg");
Balancedto alancedto = (balancedto) jsonobject. Tobean (
MSG, new Balancedto (). GetClass ());

Third, get the array from the JSON

Jsonarray Subarray = Json.getjsonarray ("msg");

Net.sf.json.JSONObject:

int leng = Subarray. Size ();

Org.json.JSONObject:

int leng = Subarray. Length ();

There are other differences can compare two API documentation, if there is an error, please ask, you can send e-mail to [email protected]

The difference between Net.sf.json.JSONObject and Org.json.JSONObject

Related Article

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.