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

Source: Internet
Author: User
Tags string to json

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

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.