Java Background Encapsulation JSON Data Learning Summary (i)

Source: Internet
Author: User

First, data encapsulation

1. The list collection is converted into JSON code
List List = new ArrayList ();
List.add ("first");
List.add ("second");
Jsonarray jsonArray2 = jsonarray.fromobject (list);
2. Map collection into JSON code
map map = new HashMap ();
map.put ("name", "JSON");
map.put ("bool", boolean.true);
map.put ("int", new Integer (1));
map.put ("arr", new string[] {"A", "B"});
map.put ("func", "function (i) {return this.arr[i];}");
jsonobject JSON = jsonobject.fromobject (map);
3. Bean converted to JSON code
Jsonobject jsonobject = jsonobject.fromobject (New Jsonbean ());
4. Convert arrays into JSON code
boolean[] Boolarray = new boolean[] {true, false, true};
Jsonarray jsonArray1 = Jsonarray.fromobject (Boolarray);
5. Conversion of general data into JSON code
Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");

Ii. Introduction to the JAR package

To include the introduction of the Json-lib package in your application, the Json-lib package also relies on the following jar packages:: http://json-lib.sourceforge.net/

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

Usage Ibid.

Jsonobject jsonobject = jsonobject.fromobject (message);
GetResponse (). Getwriter (). Write (jsonobject.tostring ());

When the data is converted to JSON, it is sent to the client using the method as above. The front end is ready to get JSON data.

can also be used List list1 = new arraylist<listdate> () listdate ListDate2 = new Listdate ();
Listdate2.setid (Examsubject.getid ());
Listdate2.setvalue (Examsubject.getsubjectname ());List1.add (LISTDATE2);  Jsonarray jsonArray1 = Jsonarray.fromobject (list1);Front Desk Recycling$.each (date, function (i, obj) {
$ ("#examName"). Append ("<option value=" + obj.id + ">" + obj.value+ "</option>");
       });Third,Jsonobject Object Use

The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON transforms each other's packages. In this example, we will use the Jsonobject class to create the Jsonobject object, and then we print the values of those objects. In order to use the Jsonobject object, we will introduce the "Net.sf.json" package. To add elements to an object, we use the put () method.

Examples:

Import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsonobjectsample {//Create Jsonobject object private s           Tatic Jsonobject Createjsonobject () {Jsonobject jsonobject = new Jsonobject ();           Jsonobject.put ("username", "Huangwuyi");           Jsonobject.put ("Sex", "male");           Jsonobject.put ("QQ", "999999999");           Jsonobject.put ("Min.score", New Integer (99));           Jsonobject.put ("nickname", "Mood in the Dream");       return jsonobject;           } public static void Main (string[] args) {Jsonobject jsonobject = Jsonobjectsample.createjsonobject ();           Output Jsonobject Object System.out.println ("object before adding a property jsonobject==>\n" +jsonobject);           The type of the interpretation output object, Boolean IsArray = Jsonobject.isarray ();           Boolean isEmpty = Jsonobject.isempty ();           Boolean isnullobject = Jsonobject.isnullobject ();          System.out.println ("IsArray:" +isarray+ "IsEmpty:" +isempty+ "Isnullobject:" +isnullobject ");    Add Property       Jsonobject.element ("Address", "Xiamen, Fujian Province");           System.out.println ("Object jsonobject==>\n after adding a property" +jsonobject);           Returns a Jsonarray object Jsonarray Jsonarray = new Jsonarray ();           Jsonarray.add (0, "This is a Jsonarray value");           Jsonarray.add (1, "another Jsonarray value");           Jsonobject.element ("Jsonarray", Jsonarray);           Jsonarray array = Jsonobject.getjsonarray ("Jsonarray");           System.out.println ("Returns a Jsonarray object ==>\n" +array);                      The value after adding Jsonarray System.out.println ("result ==>\n" +jsonobject);           Returns a string based on key, string username = jsonobject.getstring ("username");                  System.out.println ("username==>" +username);        Converts the character to Jsonobject String temp=jsonobject.tostring ();        Jsonobject object = jsonobject.fromobject (temp);    Returns the value System.out.println ("Qq==>" +object.get ("QQ") based on key after conversion; }   }

Operation Result:

The object before adding the property jsonobject==>{"username": "Huangwuyi", "Sex": "Male", "QQ": "999999999", "Min.score": ", nickname": "The Mood in the Dream"} Isarray:false Isempty:false isnullobject:false The object after adding the attribute jsonobject==>{"username": "Huangwuyi", "Sex": "Male", "QQ": " 999999999 "," Min.score ":" nickname ":" The Mood in the dream "," Address ":" Xiamen, Fujian Province "returns a Jsonarray object ==>[" This is a Jsonarray value "," Another Jsonarray value "] Results ==>{" username ":" Huangwuyi "," Sex ":" Male "," QQ ":" 999999999 "," Min.score ":," nickname ":" Dream Mood "," Address ":" Xiamen, Fujian Province "," Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]}username==> huangwuyiqq==>999999999

jsonobject Online API:http://json-lib.sourceforge.net/apidocs/jdk15/index.html

Java Background Encapsulation JSON Data Learning Summary (i)

Related Article

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.