Packagecom.example.jsonresolvetest;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.util.List;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONArray;ImportCom.alibaba.fastjson.JSONObject;ImportCom.example.jsonresolvetest.bean.ArenaBean;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v4.app.Fragment;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ListView;ImportAndroid.os.Build; Public classMainactivityextendsactionbaractivity {//This class is the parsing class//1 from the web, show the data, 2 from the local JSON, show the data PrivateListView Lv_arena; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Lv_arena=(ListView) Findviewbyid (R.id.lv_arena); Jsondata (mainactivity. This, "Jsonarena.json"in particular,/** need to be aware that the JSON file here needs to be written as "Jsonarena.json" instead of "Jsonarena" */} Public StaticJsonobject jsondata (Context context,string fileName) {//the JSON file name in the filename assets directoryJsonobject jsondate=NULL; Try {
The Jsonarena.json file is placed under the assets folder of the project InputStreamReader InputStreamReader=NewInputStreamReader (Context.getassets (). Open (FileName), "UTF-8");//I can't find the Jsonarena file.BufferedReader bufferedreader=NewBufferedReader (InputStreamReader); String Line; StringBuilder StringBuilder=NewStringBuilder (); while((Line=bufferedreader.readline ())! =NULL) {stringbuilder.append (line); } bufferedreader.close (); Inputstreamreader.close (); Jsondate=json.parseobject (Stringbuilder.tostring ());//Get Jsonobject ObjectLOG.E ("Jsondate", "= =" +jsondate); intRetarray=jsondate.getintvalue ("ret");//get the content of RETLOG.E ("Retarray", "= =" +Retarray); String Msgarray= Jsondate.getstring ("msg");//get the content of MSGLOG.E ("Msgarray", "= =" +Msgarray); Jsonarray Arenaarray=jsondate.getjsonarray ("Arena");//get Arena node, do not know how to read the log printing is clearLOG.E ("Arenaarray", "= =" +Arenaarray); List<ArenaBean> Listarenabean=json.parsearray (arenaarray.tostring (), Arenabean.class);//Series equivalent to Arenabean nodes for(Arenabean Arenabean:listarenabean) {LOG.E ("Arena.getid", "=" +Arenabean.getid ()); LOG.E ("Arena.getname", "=" +arenabean.getname ()); LOG.E ("Arena.getid", "=" +arenabean.getstatus ()); } } Catch(IOException e) {e.printstacktrace (); } returnjsondate; } }
PackageCom.example.jsonresolvetest.bean; Public classArenabean {Private intID; PrivateString name; Private intstatus; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetStatus () {returnstatus; } Public voidSetStatus (intstatus) { This. Status =status; } }
When reading local JSON, the file name under asset includes the extension