0

if i have a json file like that

{
"users": [{
        "name": "aa",
        "address": "a"
    },
    {
        "name ": "bb",
        "address": "b"
    },
    {
        "name": "cc",
        "address": "c"
    },

]}

how to read this json file and put all names in a String array in android

i used this code but in second loop it catches exception

public void loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("data.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    try {
        JSONObject obj = new JSONObject(json);
        JSONArray m_jArry = obj.getJSONArray("users");

        for (int i = 0; i < m_jArry.length(); i++) {
            JSONObject jo_inside = m_jArry.getJSONObject(i);
            names.add(jo_inside.getString("name"));
            images.add(jo_inside.getString("address"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
3
  • post the exception....log Commented Jul 19, 2017 at 20:19
  • Post exception stack trace. Commented Jul 19, 2017 at 20:23
  • No value for name @rafsanahmad007 Commented Jul 19, 2017 at 20:23

3 Answers 3

2

That Json object 2nd element in the array has space next to name you can solve by 2 ways.

    1. As your backend guys to change that
    1. You solve the problem like this,

Change getString to optString like this

for (int i = 0; i < m_jArry.length(); i++) {
            JSONObject jo_inside = m_jArry.getJSONObject(i);

            String name = jo_inside.optString("name");

            if(TextUtils.isEmpty(name)) {
                name = jo_inside.optString("name "); // that object have space
            }
        names.add(name);
        images.add(jo_inside.getString("address"));
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Your Json file contain wrong format JSON.

Remove , from the last array element

    {
        "name": "cc",
        "address": "c"
    },

The valid Json should be:

 {
    "users": [{
            "name": "aa",
            "address": "a"
        },
        {
            "name": "bb",
            "address": "b"
        },
        {
            "name": "cc",
            "address": "c"
        }

    ]
}

1 Comment

happy to help...if the answer works you are welcome to mark it as correct. Thanks
1

You have problem in your JSON string only. In second JSON name field has extra space. And also there is , after last JSON },

{
    "users": [{
            "name":"aa",
            "address":"a"
        },
        {
            "name":"bb",
            "address":"b"
        },
        {
            "name":"cc",
            "address":"c"
        }
    ]
}

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.