Last week wrote an article about the Org.json and Jsonlib of Android, and today I want to write a little bit about how I used Org.json.
Because the school project requirements to parse a complex JSON, so on the internet search, but Google a search JSON data analysis, there will be a variety of results, jsonlib, Gson, Fastjson and so on, But there is no use of org.json, in fact, Android comes with the JSON parsing package is quite good, its usage and jsonlib similar, I was the first use of Jsonlib, in the JRE environment used well, to the Android under how all Can not run (originally is packet conflict t_t, Also meet this deep pit classmate can turn over my another article about these two package conflict)
In fact, I suggest, just use the Org.json package is good, and can use a very simple line of code to complete the complex JSON parsing, initially I read some articles on the Internet, in order to parse a complex JSON file is recursive and do what, it is too laborious, there is no need.
Talk less, stick my code.
1 Packagecom.helpers;2 3 ImportJava.io.InputStream;4 5 Importorg.apache.http.util.EncodingUtils;6 ImportOrg.json.JSONArray;7 8 ImportAndroid.content.Context;9 Ten ImportCOM.EXAMPLE.TEST.R; One A Public classHelpers { - PublicString Res; - PublicJsonarray ja; the - Public voidinit (Context context) { - Try { - //reading JSON files +InputStream in =context.getresources (). Openrawresource ( - r.raw.community); + intLength =in.available (); A byte[] buffer =New byte[length]; at in.read (buffer); -res = encodingutils.getstring (buffer, "BIG5"); - in.close (); - //instantiate Jsonarray, because the outermost layer of my JSON file is an array, and if you are an object you should instantiate a Jsonobject -JA =NewJsonarray (res); - for(inti = 0; I < ja.length (); ++i) { in //Look, this is a complicated line, but it's not hard. - //my JSON file has 20 Jsonobject themes, ja.getjsonobject (i) loop read to //Each theme has a comments array that holds all the comments Getjsonarray ("comments") for this topic + //Ja.getjsonobject (i). Getjsonarray ("Comments"). Getjsonobject (i), because each comments is also jsonobject here a second getjsonobject (i ) is to get the first comments in these comments - //in comments This object also has a jsonobject called author, it contains comments information, so through Getjsonobject ("author") the //then this is the last layer, and then down there is no son of Jsonobject or Jsonarray, getString ("Profile_image"), done *System.out.println (Ja.getjsonobject (i). Getjsonarray ("Comments"). Getjsonobject (i) $. Getjsonobject ("Author"). GetString ("Profile_image").));Panax Notoginseng } -}Catch(Exception e) { the e.printstacktrace (); + } A the } +}
If you understand this example, you should understand that Jsonarray can be nested jsonobject, can also nest Jsonarray, conversely, jsonobject can also nest array or object. No matter how complex JSON files are, parsing is done through Getjsonobject or Getjsonarray. The parsing of JSON is all about layer-by-layer acquisition (Getjsonobject,getjsonarray), which is the object or array that you want to get the data from, and then GetString.
To make sense of this, then complex JSON data file, as long as a line of code, minutes to solve it!
A single line of code parsing a complex JSON file: Parsing JSON with a package from Android