user7423754
user7423754

Reputation:

Decode JSON String in Android

I have a contacts which is in the form of JSON. Now I want to decode them into String array. There are two arrays; names and phones. I'm using this code:

    String[] names;
    String[] phones;

    String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
    try {
        JSONArray jsonArray = new JSONArray(test);
        JSONObject jsonObject = new JSONObject(jsonArray.toString());
        Log.i("INFO", String.valueOf(jsonObject.length()));
    } catch (JSONException e) {
        e.printStackTrace();
    }

This generates an error. How can I add all names in names array and all phones in phones array. Like names[0] is assigned A which is a first name and phones[0] assigned 911 which is first phone number corresponding to first name. How can I do that, I'm new in android?

Upvotes: 1

Views: 1969

Answers (3)

Prashant
Prashant

Reputation: 1613

The problem is in this line

JSONObject jsonObject = new JSONObject(jsonArray.toString());

you are trying to convert JSONArray in JSONObject , which you can't. if you want to access JSONObject inside of JSONArray you have to loop through each, or you can get specific object by it's index.

    String[] names;
    String[] phones;

    String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
    try {
        JSONArray jsonArray = new JSONArray(test);
        phones = names = new String[jsonArray.length()];
        for(int i = 0 ; i < jsonArray.length(); i ++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            names[i] = jsonObject.getString("name");
            phones[i] = jsonObject.getString("phone");
            Log.i("INFO", "name : " + jsonObject.getString("name") + " , phone : " + jsonObject.getString("phone"));

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

Upvotes: 3

Piyush
Piyush

Reputation: 2609

You can't convert json arrya to json object like that:

try {
        JSONArray jsonArray = new JSONArray(test);
        for(int i=0; i<jsonArray.length(); i++) {
           JSONObject jsonObject = jsonArray.getJSONObject(i);
           // Your code goes here..  
        }

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

Upvotes: 1

Ankush Bist
Ankush Bist

Reputation: 1884

here is the required code.

 ArrayList<String> names = new ArrayList<>();
    ArrayList<String> phones = new ArrayList<>();

    String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
    try {
        JSONArray jsonArray = new JSONArray(test);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            names.add(name);
            phones.add(phone);
            Log.i("INFO", name + ", " + phone);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

above you have used simple arrays they can store data only of fixed size. But arrayList and List can store data of variable size or you do not need to fix their size.

Upvotes: 0

Related Questions