The use of Jsonobject and Jsonarray

Source: Internet
Author: User

The use of Jsonobject and Jsonarray

First, the jar package simple Introduction

To enable the program to execute must introduce the Json-lib package, the Json-lib package is dependent on the following jar package at the same time:

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

Second, 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 an element to an object, we use the put () method.

Package Com.hwy;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

public class Jsonobjectsample {

Create a Jsonobject object
private static 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 ("jsonobject==>" +jsonobject);

Interpreting the type of the output object
Boolean IsArray = Jsonobject.isarray ();
Boolean isEmpty = Jsonobject.isempty ();
Boolean isnullobject = Jsonobject.isnullobject ();
System.out.println ("IsArray:" +isarray+ "IsEmpty:" +isempty+ "Isnullobject:" +isnullobject ");

Join? properties
Jsonobject.element ("Address", "Xiamen, Fujian Province");
System.out.println ("==>" object after adding the attribute); +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:" +array);
Join? The value after Jsonarray
{"username": "Huangwuyi", "Sex": "Male", "QQ": "999999999"," Min.score ":" nickname ":" The Mood in the dream "," Address ":" Xiamen, Fujian Province "," Jsonarray ": [" This is a Jsonarray value "," another Jsonarray Value "]}
SYSTEM.OUT.PRINTLN ("result =" +jsonobject);

Returns a string based on key
String username = jsonobject.getstring ("username");
System.out.println ("username==>" +username);

Convert characters to Jsonobject
String temp=jsonobject.tostring ();
Jsonobject object = jsonobject.fromobject (temp);
Return value by key after conversion
System.out.println ("qq=" +object.get ("QQ"));

}

}

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

The use of Jsonobject and Jsonarray

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.