The use of Jsonobject and Jsonarray

Source: Internet
Author: User

Java, unlike PHP parsing and production JSON, is always a painful process. But using Jsonobject and Jsonarray will make the whole process relatively comfortable.

Packages to rely on: Commons-lang.jar commons-beanutils.jar commons-collections.jar Commons-logging.jar Ezmorph.jar Json-lib-2.2.2-jdk15.jar

1. Create a Jsonobject object:

Package Com.yunos.tv.video.resource.controller.web;import Java.util.arraylist;import Java.util.HashMap;import        Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Test {public static void main (string[] args) { Jsonobject and Jsonarray The difference is Jsonobject is the object form, Jsonarray is the array form//Create Jsonobject The first method jsonobject Jsonobject = new JS        Onobject ();        Jsonobject.put ("UserName", "Zhuli");        Jsonobject.put ("Age", "30");        Jsonobject.put ("Workin", "ALI");                System.out.println ("JsonObject1:" + jsonobject);        Create Jsonobject The second method hashmap<string, string> HashMap = new hashmap<string, string> ();        Hashmap.put ("UserName", "Zhuli");        Hashmap.put ("Age", "30");        Hashmap.put ("Workin", "ALI");                System.out.println ("JsonObject2:" + jsonobject.fromobject (HASHMAP));        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)); If Jsonarray parses a HASHMAP, the entire object is placed in the value of an array System.out.println ("Jsonarray from HASHMAP:" + jsonarray.fromobject (hash                MAP));        Assemble a complex jsonarray jsonobject jsonObject2 = new Jsonobject ();        Jsonobject2.put ("UserName", "Zhuli");        Jsonobject2.put ("Age", "30");        Jsonobject2.put ("Workin", "ALI");        Jsonobject2.element ("Array", arrayList);                    System.out.println ("JsonObject2:" + jsonObject2); }}

Results:

jsonobject1:{"UserName": "Zhuli", "Age": "A", "Workin": "Ali"}jsonobject2:{"Workin": "Ali", "Age": "A", "UserName": " Zhuli "}jsonarray1:[" Zhuli "," "," Ali "]jsonarray2:[" Zhuli "," a "," Ali "]jsonarray from hashmap:[{" Workin ":" Ali "," Age ":", "UserName": "Zhuli"}]jsonobject2:{"UserName": "Zhuli", "Age": "30", "Workin": "ALI", "Array": ["Zhuli", "", "ALI"]}

Parse JSON string:

Package Com.yunos.tv.video.resource.controller.web;import Net.sf.json.jsonarray;import Net.sf.json.JSONObject; public class Test {public    static void Main (string[] args) {        String jsonstring = "{\" username\ ": \" zhuli\ ", \" Age\ " : \ "30\", \ "workin\": \ "ali\", \ "array\": [\ "Zhuli\", \ "30\", \ "Ali\"]} ";        Convert the JSON string to a Java object        jsonobject obj = Jsonobject.fromobject (jsonstring);        Get UserName        if (Obj.has ("UserName")) {            System.out.println ("UserName:" + obj.getstring ("UserName") in Object );        }        Get Arrayobject        if (Obj.has ("array")) {            Jsonarray Transitlistarray = Obj.getjsonarray ("array");            for (int i = 0; i < transitlistarray.size (); i++) {                System.out.print ("Array:" + transitlistarray.getstring (i) + " ");            }        }    }}


Return:

username:zhuliarray:zhuli array:30 Array:ali 

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.