When reading local JSON, the file name under asset includes the extension

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.