Http://www.cnblogs.com/zhujiabin/p/5498272.html
First, what is JSON?
JSON is a data structure that replaces XML, which is smaller and less descriptive than XML, and because of its compactness, the network transmits data to reduce traffic and speed up.
JSON is a string of strings except that the elements are labeled with a particular symbol.
{} Double brackets represent objects
[] brackets represent the array
"" In double quotes is a property or value
: A colon indicates that the latter is a value (the value can be a string, a number, or another array or object)
So {"name": "Michael"} can be understood as an object containing the name Michael
And [{"Name": "Michael"},{"name": "Jerry"}] represents an array containing two objects
Of course, you can also use {"Name": ["Michael", "Jerry"]} to simplify the above, which is an object with a name array
Second, JSON parsing of the traditional JSON parsing 1, generating a JSON string
public static string Createjsonstring (string key, Object value) { Jsonobject jsonobject = new Jsonobject (); Jsonobject.put (key, value); return jsonobject.tostring ();}
2. Parsing JSON strings
In the following three cases, a javabean, a list array, a list array of nested maps:
Import Java.util.arraylist;import java.util.hashmap;import Java.util.iterator;import Java.util.List;import Java.util.map;import org.json.jsonarray;import Org.json.jsonobject;import com.android.myjson.domain.person;/** * Complete parsing of JSON data * */public class Jsontools {public static person Getperson (string key, String jsonstring) {Perso n person = new person (); try {jsonobject jsonobject = new Jsonobject (jsonstring); Jsonobject personobject = jsonobject.getjsonobject ("person"); Person.setid (Personobject.getint ("id")); Person.setname (personobject.getstring ("name")); Person.setaddress (personobject.getstring ("Address")); } catch (Exception e) {//Todo:handle Exception} return person; public static list Getpersons (string key, String jsonstring) {List List = new ArrayList (); try {jsonobject jsonobject = new Jsonobject (jsonstring); Returns an array of JSON Jsonarray Jsonarray = Jsonobject.getjsonarray (key); for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonObject2 = Jsonarray.getjsonobject (i); person person = new person (); Person.setid (Jsonobject2.getint ("id")); Person.setname (jsonobject2.getstring ("name")); Person.setaddress (jsonobject2.getstring ("Address")); List.add (person); }} catch (Exception e) {//Todo:handle Exception} return list; public static list GetList (string key, String jsonstring) {List List = new ArrayList (); try {jsonobject jsonobject = new Jsonobject (jsonstring); Jsonarray Jsonarray = Jsonobject.getjsonarray (key); for (int i = 0; i < jsonarray.length (); i++) {String msg = jsonarray.getstring (i); List.add (msg); }} catch (Exception e) { Todo:handle exception} return list; } public static list> Listkeymaps (string key, String jsonstring) {list> List = new ARRAYLIST&G t; (); try {jsonobject jsonobject = new Jsonobject (jsonstring); Jsonarray Jsonarray = Jsonobject.getjsonarray (key); for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonObject2 = Jsonarray.getjsonobject (i); Map map = new HashMap (); Iterator Iterator = Jsonobject2.keys (); while (Iterator.hasnext ()) {String Json_key = Iterator.next (); Object Json_value = Jsonobject2.get (Json_key); if (Json_value = = null) {Json_value = ""; } map.put (Json_key, Json_value); } list.add (map); }} catch (Exception e) {//Todo:handle Exception } return list; }}
GSON1 JSON parsing, generating JSON strings
Import Com.google.gson.gson;public class Jsonutils {public static String createjsonobject (Object obj) {Gson GS on = new Gson (); String str = Gson.tojson (obj); return str; }} II, parsing jsonimport java.util.arraylist;import java.util.list;import java.util.map;import com.google.gson.Gson;import Com.google.gson.reflect.TypeToken;; public class Gsontools {public Gsontools () {//TODO auto-generated constructor stub}/** * @param * @param jsonstring * @param CLS * @return */public static T Getperson (String jsonstring, Class CLS) {T t = null; try {Gson Gson = new Gson (); t = Gson.fromjson (jsonstring, CLS); } catch (Exception e) {//Todo:handle Exception} return t; /** * Use Gson to parse List * * @param * @param jsonstring * @param CLS * @return */Publi C Static List getpersons (String jsonstring, Class cls) {List List = new ArrayList (); try {Gson Gson = new Gson (); List = Gson.fromjson (jsonstring, New typetoken> () {}.gettype ()); } catch (Exception e) {} return list; }/** * @param jsonstring * @return * * public static List getList (String jsonstring) {List List = new ArrayList (); try {Gson Gson = new Gson (); List = Gson.fromjson (jsonstring, New typetoken> () {}.gettype ()); } catch (Exception e) {//Todo:handle Exception} return list; public static list> Listkeymaps (String jsonstring) {list> List = new arraylist> (); try {Gson Gson = new Gson (); List = Gson.fromjson (jsonstring, New typetoken>> () {}.gettype ()); } catch (Exception e) {//Todo:handle Exception} return list; }}
Fastjson of JSON parsing
Import Java.util.arraylist;import java.util.list;import Java.util.map;import Com.alibaba.fastjson.json;import Com.alibaba.fastjson.typereference;public class Jsontool {public static T Getperson (String jsonstring, class CLS) { T t = null; try {t = json.parseobject (jsonstring, CLS); } catch (Exception e) {//Todo:handle Exception} return t; public static list Getpersonlist (String jsonstring, Class cls) {List List = new ArrayList (); try {list = Json.parsearray (jsonstring, CLS); } catch (Exception e) {//Todo:handle Exception} return list; } public static list> GetPersonListMap1 (String jsonstring) {list> List = new Arraylist> () ; try {list = Json.parseobject (jsonstring, New typereference>> () {} . GetType ()); } catch (Exception e) {//Todo:hAndle exception} return list; }}
Summarize:
JSON for mobile devices, especially in the case of poor network environment and traffic constraints, data transmission relative to the XML format will be more efficient traffic and transmission efficiency. Fastjson is the most efficient of these three analytic methods and is recommended for use.
Three ways to parse JSON