The use of Jsonobject and Jsonarray Jsonobject contains Jsonarray

Source: Internet
Author: User

A lot of people use json-lib, to use so many libraries is very troublesome Ah, direct eclipse inside with Android's own JSON is good.

It's simple, I'll put the code down.

Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;

Look, this is a self-bringing JSON.

Jsonobject param = new Jsonobject ();
for (int j = 0; J < Postparams.length; J + +) {
//This is the addition of Jsonarray postparams[j].values is specified by a arraylist<string> that is groupids.
The red is the list I made in front of me.

for (Alarmgroupbean Chen:alarmgroupbeans) {

if ( Chen.getchecked ()) {
Groupids.add (Chen.getuid ());
}
}
< /span>param.put (postparams[j].name, 
new Jsonarray (Postparams[j]. values));
log.v ("Czyczy", postparams[j].name+ "zzzz" +postparams[j].values+ "CCCCCC ");
}
//This is a generic string key-value pair
{ param.put (postparams[j].name, 
Span style= "White-space:pre" > Postparams[j].value);
log.v ("Czyczy", postparams[j].name+ "zzzz" +postparams[j].value+ "CCCCCC" );
}

LOG.V ("Czyczy", param.tostring ());


This post was transferred from http://blog.csdn.net/huangwuyi/article/details/5412500

The use of Jsonobject and Jsonarray

Introduction of 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

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 elements 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 ");

Add Property
Jsonobject.element ("Address", "Xiamen, Fujian Province");
System.out.println ("Object ==> 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:" +array);
Value after adding Jsonarray
{"username": "Huangwuyi", "Sex": "Male", "QQ": "999999999", "Min.score": "The Heart of 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 after conversion based on key
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 Jsonobject contains 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.