Use of JSONObject and JSONArray in Android
The Android jdk contains several JSON APIs:
You can also download the JSON package: http://files.cnblogs.com/java-pan/lib.rar
JSONObject online API: http://json-lib.sourceforge.net/apidocs/jdk15/index.html
To run a program, the JSON-lib package must be introduced. The JSON-lib package depends on the following JAR package:
Commons-lang.jar
Commons-beanutils.jar
Commons-collections.jar
Commons-logging.jar
Ezmorph. jar
Json-lib-2.2.2-jdk15.jar
1. JSONObjectThe form is similar to Map, which is composed of key-value pairs, except that JSONObject uses the colon ":" And Map uses a comma.
A json object is a key that corresponds to a value. braces {} are used, for example, {key: value}
Example: JSON object
{
"Publisher": "People's post and telecommunications Publishing House ",
"Image": "http://img3.doubanio.com \/mpic \/s27308766.jpg ",
"Isnn10": "7115314640 ",
"Isbn13": "9787115314642 ",
"Title": "Android development basics and practices ",
"Url": "http: // api.douban.com \/v2 \/book \/24701842 ",
"Price": "59.00 yuan"
}
The JSONObject class is a final class that inherits the Object and implements the JSON interface.
The constructor is as follows:
JSONObject (); creates an empty JSONObject
JSONObject (boolean isNull); creates a JSONObject that is empty or not.
The common method is as follows:
FromBean (Object bean); static method. Create a JSONObject Object through a pojo Object
FromJSONObject (JSONObject object); static method, constructs a JSONObject object through another JSONObject object
FromJSONString (JSONString string); static method. A JSONString is used to create a JSONObject object.
ToString (); converts a JSONObject object to a json string.
Iterator (); returns an Iterator object to traverse elements.
2, JSONArray, Json array, using brackets [], but the items in the array are also in the format of json key-value pairs
Example: ["string1", "string2"]., [jsonobject1, jsonobject2, ......]
JSONArray is a final class that inherits the Object and implements the JSON interface.
The constructor is as follows:
JSONArray (); construct an empty JSONArray object
The common method is as follows:
FromArray (Object [] array); static method, creates a JSONArray Object through a java array
FromCollection (Collection collection); static method, creates a JSONArray object through the collection object
FromString (String string); static method. A JSONArray object is constructed using a json String.
ToString (); converts a JSONArray object to a string in json format
Iterator (); returns an Iterator object to traverse elements.
A simple example of XML and JSON string columns:
JSON: {"password": "123456", "username": "Zhang San"} XML: // The XML parsing technology: DOM, SAX, and PUll
123456
Zhang San