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
JSON parsing of the traditional JSON parsing1. Generating a JSON string
Public Static string createjsonstring (String key, Object value) { 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 classJsontools { Public StaticPerson Getperson (string key, String jsonstring) { person person=NewPerson (); Try{jsonobject Jsonobject=NewJsonobject (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 } returnPerson ; } Public Staticlist getpersons (string key, String jsonstring) {List List=NewArrayList (); Try{jsonobject Jsonobject=NewJsonobject (jsonstring); //returns an array of JSONJsonarray Jsonarray =Jsonobject.getjsonarray (key); for(inti =0; I < jsonarray.length (); i++) {Jsonobject jsonObject2=Jsonarray.getjsonobject (i); Person Person=NewPerson (); Person.setid (Jsonobject2.getint ("ID")); Person.setname (Jsonobject2.getstring ("name")); Person.setaddress (Jsonobject2.getstring ("Address")); List.add (person); } } Catch(Exception e) {//Todo:handle Exception } returnlist; } Public Staticlist getList (string key, String jsonstring) {List List=NewArrayList (); Try{jsonobject Jsonobject=NewJsonobject (jsonstring); Jsonarray Jsonarray=Jsonobject.getjsonarray (key); for(inti =0; I < jsonarray.length (); i++) {String msg=jsonarray.getstring (i); List.add (msg); } } Catch(Exception e) {//Todo:handle Exception } returnlist; } Public StaticList>listkeymaps (String key, String jsonstring) {List> list =NewArraylist>(); Try{jsonobject Jsonobject=NewJsonobject (jsonstring); Jsonarray Jsonarray=Jsonobject.getjsonarray (key); for(inti =0; I < jsonarray.length (); i++) {Jsonobject jsonObject2=Jsonarray.getjsonobject (i); Map Map=NewHashMap (); 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 } returnlist; }}
Gson of JSON parsing1. Generating a JSON string
import Com.google.gson.Gson; Public classJsonutils { Public StaticString createjsonobject (Object obj) {Gson Gson=NewGson (); String Str=Gson.tojson (obj); returnstr; }} 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 classGsontools { PublicGsontools () {//TODO auto-generated Constructor stub } /** * @param * @param jsonstring * @param cls * @return*/ Public StaticT Getperson (String jsonstring, Class cls) {T T=NULL; Try{Gson Gson=NewGson (); T=Gson.fromjson (jsonstring, CLS); } Catch(Exception e) {//Todo:handle Exception } returnT; } /** * use Gson to parse List * * @param * @param jsonstring * @param cls * @return*/ Public Staticlist getpersons (String jsonstring, Class cls) {List List=NewArrayList (); Try{Gson Gson=NewGson (); List= Gson.fromjson (jsonstring,NewTypetoken>() {}.gettype ()); } Catch(Exception e) {}returnlist; } /** * @param jsonstring * @return*/ Public Staticlist getList (String jsonstring) {List List=NewArrayList (); Try{Gson Gson=NewGson (); List= Gson.fromjson (jsonstring,NewTypetoken>() {}.gettype ()); } Catch(Exception e) {//Todo:handle Exception } returnlist; } Public StaticList>listkeymaps (String jsonstring) {List> list =NewArraylist>(); Try{Gson Gson=NewGson (); List=Gson.fromjson (jsonstring,NewTypetoken>>() {}.gettype ()); } Catch(Exception e) {//Todo:handle Exception } returnlist; }}
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 classJsontool { Public StaticT Getperson (String jsonstring, Class cls) {T T=NULL; Try{T=Json.parseobject (jsonstring, CLS); } Catch(Exception e) {//Todo:handle Exception } returnT; } Public Staticlist getpersonlist (String jsonstring, Class cls) {List List=NewArrayList (); Try{List=Json.parsearray (jsonstring, CLS); } Catch(Exception e) {//Todo:handle Exception } returnlist; } Public StaticList>GetPersonListMap1 (String jsonstring) {List> list =NewArraylist>(); Try{List=Json.parseobject (jsonstring,NewTypereference>>() {}.gettype ()); } Catch(Exception e) {//Todo:handle Exception } returnlist; }}
Summary:
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