the definition of JSON:
A lightweight data interchange format with good readability and fast-coding features. The industry's mainstream technology provides it with a complete solution (a bit like regular expressions that are supported by most languages today), allowing data exchange between platforms. JSON uses a highly compatible text format, as well as behavior similar to the C language system.
JSON Vs XML
1.JSON and XML are basically the same readability of data
2.JSON and XML also have rich analytical tools
3.JSON the volume of data is small relative to XML
4.JSON interaction with JavaScript is more convenient
5.JSON is less descriptive of data than XML
6.JSON is much faster than XML
android2.3 provides the JSON parsing class of the JSON parsing section of Android is under the package Org.json, mainly in the following categories:
Jsonobject: Can be thought of as a JSON object, which is a basic unit of JSON definition in the system that contains a pair of child (Key/value) values. Its response to an external (External: The value of the output of the application ToString () method) is reflected as a standard string (for example: {"JSON": "Hello, World"}, wrapped in curly braces, where key and value are separated by a colon ":"). It has a slight operational format for internal (Internal) behavior, such as initializing a Jsonobject instance, and referencing the internal put () method to add a value: New Jsonobject (). Put ("JSON", "Hello, world!"), The key and value are separated by commas ",". The types of value include: Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.
The Jsonstringer:json text building class, which, according to the official explanation, can help create JSON text quickly and easily. The biggest advantage of this is that you can reduce the error in the format resulting in a program exception, referencing this class can automatically create JSON text strictly according to the JSON syntax rules (syntax rules). Only one JSON text can be created for each Jsonstringer entity. The biggest advantage of this is that you can reduce the error in the format resulting in a program exception, referencing this class can automatically create JSON text strictly according to the JSON syntax rules (syntax rules). Only one JSON text can be created for each Jsonstringer entity.
Jsonarray: It represents a set of ordered values. The form of converting it to string output (toString) is wrapped in square brackets, with values separated by commas "," (for example, [Value1,value2,value3], where you can use the short code to get a more intuitive understanding of its format). The interior of this class also has query behavior, and both the Get () and opt () methods can return the specified value through the index index, and the put () method is used to add or replace the numeric value. The value type of the same class can include Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object. Jsontokener:json Resolution Class
The exception used in the Jsonexception:json
Here's an example of JSON generation and parsing:
/** * JSON operation class.
* @author E */public class Jsonutil {/** * Converts an array to JSON-formatted data. * @param stonelist Data source * @return JSON-formatted data */public static String Changearraydatetojson (arraylist<stone> Stone
List) {try {jsonarray array = new Jsonarray ();
Jsonobject object = new Jsonobject ();
int length = Stonelist.size ();
for (int i = 0; i < length; i++) {Stone stone = stonelist.get (i);
String name = Stone.getname ();
String size = Stone.getsize ();
Jsonobject stoneobject = new Jsonobject ();
Stoneobject.put ("name", name);
Stoneobject.put ("size", size);
Array.put (Stoneobject);
} object.put ("Stones", array);
return object.tostring ();
catch (Jsonexception e) {e.printstacktrace ();
return null;
/** * Converts JSON to an array and returns. * @param Json * @return arraylist<stone> */public static arraylist<stone> Changejsontoarray (String Json)
{arraylist<stone> gamelist = new arraylist<stone> ();
try {Jsonobject jsonobject = new Jsonobject (Json);
if (!jsonobject.isnull ("stones")) {String astring = jsonobject.getstring ("stones");
Jsonarray Ajsonarray = new Jsonarray (astring);
int length = Ajsonarray.length ();
for (int i = 0; i < length; i++) {Jsonobject Stonejson = Ajsonarray.getjsonobject (i);
String name = stonejson.getstring ("name");
String size = stonejson.getstring ("size");
Stone stone = new Stone ();
Stone.setname (name);
Stone.setsize (size);
Gamelist.add (stone);
A catch (Exception e) {e.printstacktrace ());
return gamelist; }
}
After you have written a good method, you can refer to it as follows:
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
public class Mainactivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Findviewbyid (r.id.test). Setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick ( View v) {
init ();}}
);
private void Init () {
arraylist<stone> list = new arraylist<stone> ();
for (int i = 0; i < 5; i++) {
Stone stone = new Stone ();
Stone.setname ("Name" +i);
Stone.setsize ("Size" + i);
List.add (stone);
String json = Jsonutil.changearraydatetojson (list);
LOG.E ("JSON", JSON);
}
Looking at the log again, you see the generated JSON data:
{' Stones ': [{' Size ': ' Size0 ', ' name ': ' NAME0 '},{' size ': ' Size1 ', ' name ': ' Name1 '},{' size ': ' Size2 ', ' name ': ' Name2 '},{' Size ': ' Size3 ', ' name ': ' Name3 '},{' size ': ' Size4 ', ' name ': ' Name4 '}]}
The above is how to generate JSON data, please refer to the second method.