1
try {
        File yourFile = new File(Environment.getExternalStorageDirectory(), "textarabics.txt");
        FileInputStream stream = new FileInputStream(yourFile);
        String jsonStr = null;
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

            jsonStr = Charset.defaultCharset().decode(bb).toString();
            Log.d("Noga Store", "jString = " + jsonStr);
          }
          finally {
            stream.close();
          }

        Log.d("Noga Store", "jString = " + jsonStr);
             JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting data JSON Array nodes
            JSONArray data  = jsonObj.getJSONArray("data");

            // looping through All nodes
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                String id = c.getString("id");
                String title = c.getString("title");
                String duration = c.getString("duration");

                // tmp hashmap for single node
                HashMap<String, String> parsedData = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                parsedData.put("id", id);
                parsedData.put("title", title);
                parsedData.put("duration", duration);


                // do what do you want on your interface
              }


       } catch (Exception e) {
       e.printStackTrace();
      }

at that point i am getting crash:

JSONObject jsonObj = new JSONObject(jsonStr);

and this is my json file into my sd card:

{
"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,
    }
]

}
4
  • Post your json string. Commented Feb 23, 2014 at 14:27
  • @RajeshCP edit my ans plz checked Commented Feb 23, 2014 at 14:28
  • Log.d("Noga Store", "jString = " + jsonStr); what is it printing in logcat ? Commented Feb 23, 2014 at 14:29
  • { "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, } ] } Commented Feb 23, 2014 at 14:30

2 Answers 2

2
{ "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 JSON is invalid "duration": 10, there is an extra comma at the end. Remove that comma and try.

Remove that comma from every object. The modified JSON will look like 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
    }
]

}
Sign up to request clarification or add additional context in comments.

4 Comments

bro i remove the commas in every object and still got a crash org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
Check the log part now how the JSON String is coming.
in log this is the output now: { "data": [ { "id": "1", "title": "Farhan Shah", "duration": 10 }, { "id": "2", "title": "Noman Shah", "duration": 3 }, { "id": "3", "title": "Ahmad Shah", "duration": 5 }, { "id": "4", "title": "Mohsin Shah", "duration": 7 }, { "id": "5", "title": "Haris Shah", "duration": 4 } ] }
tutorialspoint.com/json/json_java_example.htm foloow this link about teh creation of JSON. You JSON is not valid still.
0
String jsonStr = "";

instead of

String jsonStr = null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.