Conversion of JSON objects to JSON strings, JSON strings to Java objects
I. Conversion of JSON objects to JSON strings
1.How the jquery plugin supports conversion :
$.parsejson (jsonstr); Jquery.parsejson (jsonstr), You can convert a JSON string into a JSON object
2. Browser supported conversion mode (FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8) and other browsers:
Json.stringify (obj) converts the JSON to a string. Json.parse (string) to convert the string to JSON format;
var a={"name": "tom", "sex": "male", "age": "24"};
var b= ' {' name ': ' Mike ', ' sex ': ' female ', ' age ': ' 29 '} ';
var atostr=json.stringify (a);
var btoobj=json.parse (b);
Alert (typeof (atostr)); String
Alert (typeof (btoobj));//object
3.how JavaScript supports conversion :
Eval (' (' + jsonstr + ') '); You can convert a JSON string to a JSON object, noting that you need to wrap a pair of parentheses outside the JSON character
Note: IE8 (compatibility mode), IE7 and IE6 can also use eval () to convert strings to JSON objects, but these methods are not recommended, which is unsafe for eval to execute an expression in a JSON string.
4.JSON Official Conversion Mode :
Http://www.json.org/provides a json.js so that IE8 (compatibility mode), IE7 and IE6 can support JSON objects as well as their stringify () and parse () methods;
Can get this JS on https://github.com/douglascrockford/JSON-js, generally now use json2.js.
Ii. conversion of JSON strings to Java objects
1. Convert the list of Java objects to an array of JSON objects and move to a string
Jsonarray array = Jsonarray.fromobject (list);
String jsonstr = array.tostring ();
2. Converting Java objects to JSON objects and converting them into strings
Jsonobject object = Jsonobject.fromobject (user);
Log4jInit.ysulogger.debug (object.tostring ());
3. Convert the JSON string to an array of Java objects
Jsonarray JSON = Jsonarray.fromobject (userstr);//userstr is a JSON string
list<user> users= (list<user>) jsonarray.tocollection (json, user.class);
4. Convert the JSON string to a Java object
Jsonobject Jsonobject = Jsonobject.fromobject (jsonstr);
User user= (user) Jsonobject.tobean (object,user.class);
Conversion of JSON objects to JSON strings, JSON strings to Java objects