JSON to JS object, JS object to JSON

Source: Internet
Author: User

A JSON string from the server, how can it be called as a JavaScript object (JSON object) on the Web side?

1, if using jquery, it is very convenient, you can in the AJAX series of functions, the parameter datatype JSON can be, the returned data is a JSON object.

PS: If you want to process the form as a JSON string, you can use. Serialize () and. Serializearray () processing, if you want to call as a URL, you can use Jquery.param () processing.

2, $.parsejson (JSONSTR)

3. Json.parse (JSONSTR) from the browser

4. Using the string transcoding function

Eval (' (' + jsonstr + ') '); (not recommended, code executed)

5, using the official JSON json.js

Parse () method

Second, JSON object to string

1, json.stringify (jsonobj);

2, Obj.tojsonstring ()

Transferring to Java objects

1.//Convert the list of Java objects to a JSON object array and turn to a string

Jsonarray array = jsonarray.fromobject(userlist);
String jsonstr = array.tostring ();

2.//Converts a Java object into a JSON object and converts it into a string

Jsonobject object = jsonobject.fromobject(invite);
Log4jInit.ysulogger.debug (Object.ToString ());

3.//converting a JSON string to an array of Java objects

String personstr = Getrequest (). GetParameter ("persons");
Jsonarray json = Jsonarray.fromobject (PERSONSTR);
list<invoidperson> persons = (list<invoidperson>)jsonarray.tocollection(JSON, Nvoidperson.class);
4.//to convert a JSON string to a Java object

str = "{\" lendperson\ ": \" John Doe \ ", \" lendcompany\ ": \" limited \ ", \" checkperson\ ": \" John Doe \ ",
\ "lenddate\": \ "2010-07-19t00:00:00\", \ "lendcounts\": 4,\ "
Passports\ ": [{\" passportid\ ": \" d\ ", \" name\ ": \" Li Shuichang \ ", \" passporttype\ ": \" K\ "},
{\ "passportid\": \ "k9051\", \ "name\": \ "Li ping \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k90517\", \ "name\": \ "Yuan Han mei \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k905199\", \ "name\": \ "He Ming \", \ "passporttype\": \ "K\"}]} ";
Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Get a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Convert JSON arrays to list<passportforlendsentity> generics
list<passportforlendsentity> list = new arraylist<passportforlendsentity> ();
for (int i = 0; i < array.size (); i++) {
Jsonobject object = (jsonobject) array.get (i);
Passportforlendsentity Passport = (passportforlendsentity)Jsonobject.tobean(Object,
Passportforlendsentity.class);
if (passport! = null) {
List.add (Passport);
}
}
Convert passportlendsentity entity class
Passportlends = (passportlendsentity)Jsonobject.tobean(Jsonobject, Passportlendsentity.class);

JSON to JS object, JS object to JSON

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.