1

I print this json array in my android

It includes an array inside of an array

What is the android code?

This is my php output

[{"result":"true","user":[{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}]}]
Draken
  • 3,134
  • 13
  • 34
  • 54
Darshi Domadiya
  • 267
  • 3
  • 13

4 Answers4

2

try to remove all array brackets [ ] because you need JSON Object only not array of json object. then data will look like

{"result":"true","user":{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}}

now android code:

try {
    JSONObject json = new JSONObject(result);
    JSONObject user= json.getJSONObject("user");
    String name= user.getString("name");     
} catch (JSONException e) {
    e.printStackTrace();
}
Sharad Kale
  • 971
  • 1
  • 7
  • 19
  • 1
    why to remove bracketes ? – Jaimin Modi May 23 '17 at 13:17
  • its ok, but why dont you using JSONARRAY class to pass the actual JSON String because it contains [] brackets. no need to remove it. – Jaimin Modi May 23 '17 at 13:20
  • Partly true, but I think it's harder to remove the first and last bracket from a string (*also make sure you remove brackets!*), than use a JSONArray instead of a JSONObject. – Denny May 23 '17 at 13:21
  • @Denny nothing is harder. just we need proper knowledge about it. – Jaimin Modi May 23 '17 at 13:23
  • what about if you have something like this: `"data":{"1":{"id":1,"name":"Bitcoin","symbol":"BTC","category":"coin","description":"this is a description"..` Im trying to do the same but after "data": {"1":{id:1... is more difficult – Maduro Jul 24 '21 at 20:59
2
try {
        //send response
        JSONArray jsonArray = JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if(!jsonObject.isNull("result")){
                String result = jsonObject.getString("result");
                if(result.equals("true")){
                    JSONArray userArray = jsonObject.getJSONArray("user");
                    for(int j=0; j<userArray.length(); j++){
                        jsonObject = userArray.getJSONObject(j);
                        String id = isJsonValid(jsonObject, "id");
                        String name = isJsonValid(jsonObject, "name");
                        String email = isJsonValid(jsonObject, "email");
                        String password = isJsonValid(jsonObject, "password");
                        String address = isJsonValid(jsonObject, "address");
                        String city = isJsonValid(jsonObject, "city");
                        String contact = isJsonValid(jsonObject, "contact");
                        String qualification = isJsonValid(jsonObject, "qualification");
                        String experience = isJsonValid(jsonObject, "experience");
                        String department = isJsonValid(jsonObject, "department");
                        String to_time = isJsonValid(jsonObject, "to_time");
                        String age = isJsonValid(jsonObject, "age");
                        String from_time = isJsonValid(jsonObject, "from_time");
                        String status = isJsonValid(jsonObject, "status");
                        String current_date_time = isJsonValid(jsonObject, "current_date_time");
                    }
                }else{
                    //do something
                }
            }
        }
    }catch (JSONException je){
        je.printStackTrace();
    }


public String isJsonValid(JSONObject jsonObject, String key) throws JSONException {
    //check key is present or not
    if(!jsonObject.isNull(key)){
       return jsonObject.getString(key);
    }else{
        return "N/A";
    }
}
Mohammad nabil
  • 1,010
  • 2
  • 12
  • 23
  • Thanks for help me – Darshi Domadiya May 24 '17 at 03:53
  • what about if you have something like this: `"data":{"1":{"id":1,"name":"Bitcoin","symbol":"BTC","category":"coin","description":"this is a description"..` Im trying to do the same but after "data": {"1":{id:1... is more difficult – Maduro Jul 24 '21 at 20:59
2
String data="[{"result":"true","user":[{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}]}]";
        try
        {
            JSONArray array=new JSONArray(data);
            JSONObject jsonObject=array.getJSONObject(0);
            String returnword=jsonObject.getString("return");
            JSONArray users=jsonObject.getJSONArray("user");
            for(int i=0;i<users.length();i++)
            {
                JSONObject object=users.getJSONObject(i);
                String id=object.getString("id");
                String name=object.getString("name");
                String email=object.getString("email");
                String password=object.getString("password");
                String address=object.getString("address");
                String city=object.getString("city");
                String contact=object.getString("contact");
                String qualification=object.getString("qualification");
                String experience=object.getString("experience");
                String department=object.getString("department");
                String age=object.getString("age");
                String from_time=object.getString("from_time");
                String to_time=object.getString("to_time");
                String status=object.getString("status");
                String current_date_time=object.getString("current_date_time");

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

This is the simple way to parse any kind of json.

MageNative
  • 682
  • 5
  • 14
  • what about if you have something like this: `"data":{"1":{"id":1,"name":"Bitcoin","symbol":"BTC","category":"coin","description":"this is a description"..` Im trying to do the same but after "data": {"1":{id:1... is more difficult – Maduro Jul 24 '21 at 20:57
1

You can make use of Gson library

Step 1: Create the POJO for the json data

public class Data {
    public String result;
    public List<User> user;
}

public class User {
    public String status;
    public String department;
    public String password;
    public String contact;
    public String city;
    public String id;
    public String qualification;
    public String email;
    public String address;
    public String from_time;
    public String name;
    public String age;
    public String current_date_time;
    public String experience;
    public String to_time;
}

Step 2: Parse the JSON using Gson library

final Gson gson = new Gson();

Since it's a array of data, we need to get a List like this from the json string

List<Data> response = gson.fromJson(JSON_DATA, new TypeToken<List<Data>>() {}.getType());

Step 3: Get the values

Data data = response.get(0);
User user = data.user.get(0);

And print the result likewise

Log.d("JsonExample","Name: " + user.name);
...
...
Hussain
  • 1,243
  • 12
  • 21
  • what about if you have something like this: `"data":{"1":{"id":1,"name":"Bitcoin","symbol":"BTC","category":"coin","description":"this is a description"..` Im trying to do the same but after "data": {"1":{id:1... is more difficult – Maduro Jul 24 '21 at 20:59