Conversion of JSON objects to JSON strings, JSON strings to Java objects

Source: Internet
Author: User
Tags string to json

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);
Stringjsonstr = 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

Jsonobjectjsonobject = Jsonobject.fromobject (JSONSTR);
User user= (user)Jsonobject.tobean(object,user.class);

Conversion of JSON objects to JSON strings, JSON strings to Java objects

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.