The definition of JSON:
A lightweight data interchange format with good readability and easy-to-write features. The industry's mainstream technology provides a complete solution (somewhat like regular expressions, which is supported in most languages today), enabling data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language system.
JSON Vs XML
The data readability of 1.JSON and XML is basically the same
2.JSON and XML also have a wealth of analytic means
3.JSON data is small in size 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
JSON parsing classes provided by android2.3
The JSON parsing part of Android is under package Org.json, mainly in the following categories:
Jsonobject: Can be thought of as a JSON object, which is the basic unit in the system for json definition, which contains a pair of tostring () method output value key and value are colon ":" delimited). Its operation format for internal jsonobject instance, referencing the internal The put () method adds a value: New Jsonobject (). Put ("JSON", "Hello, world!"), between key and value is a comma Span lang= "en-us" > "," delimited. value types include: boolean, jsonarray, Jsonobject, number, string or default jsonobject.null object.
Jsonstringer:json text Building class, according to the official explanation, this class can help to create JSON text quickly and conveniently. The biggest advantage is that you can reduce program exceptions due to malformed formatting, and referencing this class automatically creates JSON text in strict accordance with the JSON syntax rules (syntax rules). Each Jsonstringer entity can only be created with one JSON text: The biggest advantage is that you can reduce program exceptions due to malformed formatting, and referencing this class automatically creates JSON text in strict accordance with the JSON syntax rules (syntax rules). Each Jsonstringer entity can only create one JSON text.
Jsonarray: It represents an ordered set of values. Converting it to a string output (toString) is performed in the form of a square bracket, separated by a comma "," (for example: [Value1,value2,value3], you can personally use the short code to understand its format more intuitively). The inside of this class also has query behavior, both get () and opt () can return the specified value through the index index, and the put () method is used to add or replace values. Similarly, the value type of this class can include: Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.
Jsontokener:json Parsing class
Exceptions used in Jsonexception:json
Here's an example of JSON generation and parsing:
[Java]View Plaincopy
- /**
- * JSON operation class.
- * @author E
- */
- Public class Jsonutil {
- /**
- * Converts an array to JSON-formatted data.
- * @param stonelist Data source
- * @return data in JSON format
- */
- public static String Changearraydatetojson (arraylist<stone> stonelist) {
- 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;
- }
- /**
- * Convert JSON to an array and return.
- * @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);
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return gamelist;
- }
- }
After writing a good method, you can refer to it as follows:
[Java]View Plaincopy
- 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, we see the resulting JSON data:
[Java]View Plaincopy
- {"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, parsing please refer to the second method.
Android JSON generation and parsing examples