Jsonobject and Jsonarray use

Source: Internet
Author: User

1. The Dependent JAR package

To enable the program to run, the Json-lib package must be introduced, and the Json-lib package relies on the following jar packages:

    1. Commons-lang.jar
    2. Commons-beanutils.jar
    3. Commons-collections.jar
    4. Commons-logging.jar
    5. Ezmorph.jar
    6. Json-lib-2.2.2-jdk15.jar
2.Demo
Package Json;import Java.util.arraylist;import Java.util.hashmap;import net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsondemo {public static void main (string[] args) {//The first method of creating a JSON object Jsonobject j SonObj1 = new Jsonobject () jsonobj1.put ("id", "1"), Jsonobj1.put ("name", "Rose"), Jsonobj1.put ("age", 25); System.out.println ("JsonObj1" +jsonobj1);//The second method of creating a JSON object hashmap<string, object> map = new hashmap<string, Object> (); Map.put ("id", "2"), Map.put ("name", "Zhangsan"), Map.put ("Age", "25"); Jsonobject jsonObj2 = jsonobject.fromobject (map); System.out.println ("JsonObj2" +jsonobj2);        Create a Jsonarray Method 1 Jsonarray jsonarray = new Jsonarray ();        Jsonarray.add (0, "Zhuli");        Jsonarray.add (1, "30");        Jsonarray.add (2, "ALI");                System.out.println ("JsonArray1:" + Jsonarray);        Create Jsonarray Method 2 arraylist<string> ArrayList = new arraylist<string> ();        Arraylist.add ("Zhuli");        Arraylist.add ("30");Arraylist.add ("ALI");                System.out.println ("JsonArray2:" + jsonarray.fromobject (arrayList));        Create a complex JSON object (JSON array in JSON object) jsonobject jsonObj3 = new Jsonobject ();        Jsonobj3.put ("id", 3);        Jsonobj3.put ("name", "Lisi");        Jsonobj3.put ("Age", 23);        Jsonobj3.put ("ArrayList", arrayList);                System.out.println ("JsonObj3" +jsonobj3);        Create a complex JSON array (JSON array containing JSON objects), Jsonarray jsonArray3 = new Jsonarray ();        Jsonarray3.add ("4");        Jsonarray3.add ("Wangwu");        Jsonarray3.add ("26");        Jsonarray3.add (map);               System.out.println ("JsonArray3" +jsonarray3); Convert JSON string to JSON object string jsonstring = "{\" id\ ": 3,\" name\ ": \" lisi\ ", \" age\ ": 23,\" arraylist\ ": [\" Zhuli\ ", \" 30\ ", \" A        Li\ "]}";        Jsonobject JsonObj4 = Jsonobject.fromobject (jsonstring); System.out.println (Jsonobj4.get ("id") + "--" +jsonobj4.get ("name") + "--" +jsonobj4.get ("age") + "--" +jsonobj4.get ("  ArrayList ")); }}

  

Jsonobject and Jsonarray use

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.