Newly registered this blog, just this two days to learn to write Android, need to parse the local JSON file, Google a search to see jsonlib this thing is very reliable, on the bold under the related dependency of the lump up, did not think this fell into a deep pit, online also can not find the reason, In the blood of the two days of churning, and now finally climbed out, and June to share, also as the beginning of the work, shortcomings also look correct.
For the first time to parse the JSON file of children's shoes may be online to find the method of parsing JSON files, so online have such as Jsonlib, Fastjson, Gson and other ways, so look at the dazzling.
In fact.. I want to say that Android comes with a Org.json package that can complete this task. No need to download those external packages
What's more,Jsonlib and Android's own Org.json pack are conflicting . Which means, if you use Jsonlib in the Java development environment, absolutely no problem, but if you use the Android development environment, sorry, will error
Error message:androidruntime java.lang.NoClassDefFoundError:Failed Resolution of:lnet/... Jsonarray
This is because Jsonlib and Org.json in the name of the package there is a conflict (I looked at the pitch darkness, repeatedly tangled, in the Java project with a good jsonlib, why put on Android will not do it, really a big pit)
In fact, Jsonlib is similar to Org.json's syntax,
In addition to the new object, different
Jsonarray ja = new Jsonarray (res); Org. JSON
Jsonarray ja = new Jsonarray (); Jsonlib
Ja.from (RES)
There is almost no difference between the two e.g when actually used.
ja. getjb (). Getjsonarray ("comments"). Getjsonobject (j) . GetString ("time_created"). ToString ()
1 ImportJava.io.InputStream;2 3 Importorg.apache.http.util.EncodingUtils;4 ImportOrg.json.JSONArray;5 ImportAndroid.content.Context;6 ImportCOM.EXAMPLE.TEST.R;7 8 Public classinitjson{9 PublicString Sjsonresource;Ten PublicJsonarray ja; One Public voidinit (Context context) { A Try { - //reading JSON files -InputStream in =context.getresources (). Openrawresource ( the r.raw.community); - intLength =in.available (); - byte[] buffer =New byte[length]; - in.read (buffer); +Sjsonresource = encodingutils.getstring (buffer, "BIG5"); - in.close (); +JA =NewJsonarray (sjsonresource); A in.close (); at for(inti = 0; I < ja.length (); ++i) { -Ja.getjsonobject (i). getString ("time_created"). toString (); - } -}Catch(Exception e) { - e.printstacktrace (); - } in - } to}
Last but no least
There is no need to use an external library, Android Org.json can solve all your questions about JSON file parsing, and it's not complicated!
Android:json data parsing--jsonlib and Org.json conflict