Java and JS Operation JSON

Source: Internet
Author: User
Tags string to json

JS JSON string to JSON object
// Convert a JSON-formatted string to a JSON object var t = json.parse (' {' Name ': 123} '

JSON object to JSON string
// an array of JSON objects.  //Packjson is an array  of objects = [                  {"name": "Nikita", "Password": "1111"},                  { "name": "Tony", "Password": "2222"}]; // json  to  stringvar s = json.stringify (packjson); // S is a string.  alert (packjson[// Access Object array Packjson

JSON string to JSON object in Java and Get properties

Parse JSON format string for object

String jsonstring = "[" + "{\" author\ ": \" 7\ ", \" id\ ": 358,\" title\ ": \" japan\ ", \" Pictures\ ": [{\" description\ ": \" 001\ ", \" imgpath\ ": \"/cms/u/cms/www/201203/05150720ii68.jpg\ "},{\" description\ ": \" 002\ ", \ "imgpath\": \ "/cms/u/cms/www/201203/05150720ii67.jpg\"}],\ "path\": \ "Ip\"}, "+" {\ "author \ ": \" 8\ ", \" id\ ": 359,\" title\ ": \" China\ ", \" Pictures\ ": [{\" description\ ": \" 101\ ", \" imgpath\ ": \"/cms/u/cms/www/ 201203/111111111111.jpg\ "},{\" description\ ": \" 102\ ", \" imgpath\ ": \"/cms/u/cms/www/201203/222222222222.jpg\ "}],\ "path\": \ "Ip\"}] "; Jsonarray Array=Jsonarray.fromobject (jsonstring);if(Array.size () > 0) {                            for(inti = 0; I < array.size (); i++) {Jsonobject job= Array.getjsonobject (i);//Traverse Jsonarray System.out.println ("Author:" + job.get ("author"));//get the property values in each object                           }                  }

The Java object is converted to a JSON object and gets the JSON object properties

        /**           * Java objects are converted to JSON objects and get JSON object Properties           *           /= Jsonobject.fromobject (student);          System.out.println (Jsonstu.tostring ());          System.out.println (Jsonstu.getjsonarray ("hobby"));         

JSON objects into Java objects and get Java object properties
       /**          * JSON objects are converted to Java objects and get Java object Properties           *           /= (Student) Jsonobject.tobean (Jsonstu, Student. class );          System.out.println (Stu.getname ());     
Converts a list of Java objects to a JSON object array and to a string

Jsonarray array == array.tostring ();

Convert a Java object to a JSON object

Jsonobject Object = Jsonobject.fromobject (invite);


put JSON the string is converted to JAVA Object Array

  String personstr = ""  = jsonarray.fromobject (personstr);  List<InvoidPerson> persons = (list<invoidperson>) jsonarray.tocollection (JSON, Nvoidperson.  Class

Code from the network

Java and JS Operation 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.