Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How can I read json file(text file) from SD Card and display data into textview [duplicate]

I have the following textfile into my SdCard.now i want to parse this file via Json parser,and i want to parse and read this file and display data into my textview,how i can do this?

{
"data": [
    {
        "id": "1",
        "title": "Farhan Shah",
        "duration": 10,
    },
    {
        "id": "2",
        "title": "Noman Shah",
        "duration": 10,
    },
    {
        "id": "3",
        "title": "Ahmad Shah",
        "duration": 10,
    },
    {
        "id": "4",
        "title": "Mohsin Shah",
        "duration": 10,
    },
    {
        "id": "5",
        "title": "Haris Shah",
        "duration": 10,
    }
  ]

}

This is the code:

File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file

    File file = new File(sdcard,"textarabics.txt");

    String UTF8 = "utf8";
    int BUFFER_SIZE = 8192;


    //Read text from file
    StringBuilder text = new StringBuilder();

i get the file,but now i have no idea that how i can parse the data?

like image 517
Farhan Shah Avatar asked Feb 23 '14 12:02

Farhan Shah


People also ask

What is the best way to view a JSON file?

Steps to open JSON files on Web browser (Chrome, Mozilla) Open the Web store on your web browser using the apps option menu or directly using this link. Here, type JSON View in search bar under the Extensions category. You will get the various extensions similar to JSON View to open the JSON format files.


1 Answers

get the file contents like this:

File yourFile = new File("/mnt/extSdCard/test.json");
        FileInputStream stream = new FileInputStream(yourFile);
        String jString = null;
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            /* Instead of using default, pass in a decoder. */
            jString = Charset.defaultCharset().decode(bb).toString();
          }
          finally {
            stream.close();
          }

parse and add to listview:

String[] from = new String[] {"title"};
int[] to = new int[] { R.id.name};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

try 
{
    JSONArray names = new JSONArray(jsonString);
    Log.i("MyList","Number of names " + names.length());
    for (int j = 0; j < names.length(); j++) 
    {
         JSONObject jsonObject = names.getJSONObject(j);
         HashMap<String, String> map = new HashMap<String, String>();
         map.put("title", jsonObject.getString("title"));
         fillMaps.add(map);
    }
} 
catch (Exception e) 
{
    e.printStackTrace();
}

SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
mListView.setAdapter(adapter);

Here mListView is your predefined ListView.

like image 135
Mohammad Rababah Avatar answered Sep 19 '22 18:09

Mohammad Rababah