Java JSON generation and parsing--json-lib

Source: Internet
Author: User

Class (parsing and generation of Java JSON):

ImportJava.util.HashMap;ImportJava.util.Map;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classJsontest { PublicString jsontostring () {Jsonobject users=NewJsonobject (); Jsonarray Array=NewJsonarray (); Map Map=NewHashMap (); Map.put ("Name", "Zhangsan"); Map.put ("Age", 13); Map.put ("Likerun", boolean.true); Map.put ("Height", 158.9); Jsonobject JSON=NewJsonobject ();        Json.putall (map);        Array.add (JSON); Map MAP2=NewHashMap (); Map2.put ("Name", "Lisi"); Map2.put ("Age", 18); Map2.put ("Likerun", Boolean.false); Map2.put ("Height", 180.5); Jsonobject Json2=NewJsonobject ();        Json2.putall (MAP2);        Array.add (Json2); Users.put ("Users", array); returnusers.tostring (); }     Public voidjsontoobject (String str) {//get JSON ObjectJsonobject obj = jsonobject.fromobject (str);//This method converts the JSON-formatted map String Javebean Dynabean to a JSON objectJsonarray array = Obj.getjsonarray ("Users");  for(inti = 0; I < array.size (); i + +) {map map=(MAP) array.get (i); Map.get ("Name"); Map.get ("Age"); }    }}

JS parsing json:

var jsontest = document.getElementById ("jsontest"var jsonjs =  eval ("+" + Jsontest.value+ ")");

Value of Jsonjs:

Java JSON generation and parsing--json-lib

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.