2

I've been wondering how to parse JSON Array in android ?

I've a json object like this: [{"ID":"1","email":"[email protected]","password":"password"},{"ID":"2","email":"[email protected]","password":"passward"}]

but I cannot find how to parse it, I want to access data so i could have the first mail, or the second id.

I've try around 40 differents solutions, but no luck.

Last time I tried something it was this:

private void showJSON(String json){
        try {
            JSONArray jArray = new JSONArray(json);
            String JsonString = jArray["ID"];
            tv.append(JsonString);

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

but it expected an array and not a json array. I still haven't found how to convert it into array.

I've also tried this:

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String name = jsonobject.getString("email");
}

but getJSONObject doesn't exist.

Thank you for your help.

1
  • 2
    "but getJSONObject doesn't exist" -- sure it does. Presumably, jsonarray is not a JSONArray. You may be happier using Gson, Jackson, or other more modern JSON parsing libraries. Commented Jan 5, 2016 at 22:46

5 Answers 5

1

Try this

private void showJSON(String json){
    try{
    JSONArray jsonArray = new JSONArray(json);
     for (int i = 0; i < jsonArray.length(); i++) {

       String id[i] = jsonArray.getJSONObject(i).getString("id");
       String email[i] = jsonArray.getJSONObject(i).getString("email");
     }
     }
    catch (JSONException e) {
                // Log.d("JSONException", e.toString());
            }
     }
Sign up to request clarification or add additional context in comments.

Comments

1

Use gson to deserialize the array back into an object. https://sites.google.com/site/gson/gson-user-guide

Comments

0

It is simple when using Gson: First, you create class User:

public class User
{
private String ID;
private String email;
private String password;
}

Then create UserStringJson class:

public class UserStringJson{
ArrayList<User> userArrays = new ArrayList<User>();
}

Finally, Using gson:

Gson gson = new Gson();
UserStringJson userStringJson = new UserStringJson();
userStringJson = gson.fromJson(jsonstring,
                    UserStringJson.class);
String email = userStringJson.getuserArrays().get(0).getEmail();

Comments

0

Create Custom Object User :

public class User {
    public String ID;
    public String email;
    public String password;
}

Use this class into showJson() method :

private void showJSON(String json) {
    try {
        List<User> userList = new ArrayList<>();

        JSONArray jsonArray = new JSONArray(json);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            User user = new User();
            user.ID = jsonObject.getString("id");
            user.email = jsonObject.getString("email");                 user.password = jsonObject.getString("password");
            userList.add(user);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Comments

0

Do something like this:

JSONArray arr= new JSONArray("json string");
for(int i=0;i<arr.length();i++){
JSONObject obj= arr.getJSONObject(i);
// Now use this obj to get desired values.
int ID= Integer.parseInt(obj.getString("ID"))// or use getInt method
// Similarly you can do for the rest.

}

Hope it helps.

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.