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