简体   繁体   中英

How to parse JSON in on response

I am using volley and implementing json parsing. I am doing json parsing and showing the data in the list view. I had made every thing ( getter setter,singleton class, adapter etc) but at the time of parsing i am facing difficulty, how to parse

{
"result": [{
    "id": "1",
    "name": "Prabhat",
    "email": "prabhat@gmail.com"
}, {
    "id": "2",
    "name": "Apurva",
    "email": "apu@gmail.com"
}, {
    "id": "3",
    "name": "sunny",
    "email": "sunny@mail.com"
}, {
    "id": "4",
    "name": "Creation InfoTech",
    "email": "creation@gmail.com"
}, {
    "id": "5",
    "name": "Sanjay Mishra",
    "email": "sanju19@gmail.com"
}]

}

And my java code is

 JsonObjectRequest request = new JsonObjectRequest(url_Array, new JSONObject(), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            for (int i = 0; i < response.length(); i++) {
                try {
                    Log.d(TAG, "working");
                    JSONObject object = response.getJSONObject(i);
                    // Now this is my PersonInfo class in which i have define my getters and setters
                    PersonInfo info = new PersonInfo(object.getString("id"), object.getString("name"), object.getString("email"));
                    personInfos.add(info);

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

            }
            adapter.notifyDataSetChanged();

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "Something not ok", Toast.LENGTH_SHORT).show();

        }

    });
    AppController.getInstance(getApplicationContext()).AddtoRequestQueue(request);

}

**Facing difficulty in parsing,kindly reply **

Before walking the list, you need first to get the JSONArray :

JSONArray array = response.getJSONArray("result")

Then you walk the array:

for (int i = 0; i < array.length(); i++) {...}

Try this code will help you to read your data into two string arrays then you can do whatever you want with them

JsonObjectRequest request = new JsonObjectRequest(url_Array, new JSONObject(), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
        JSONArray jsonArray = jsnobject.getJSONArray("result");
        String[] name=new String[jsonArray.length()];
        String[] email=new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                try {
                    Log.d(TAG, "working");
                    JSONObject student=jsonArray.getJSONObject(i);
                    name[i]=student.getString("name");
                    email[i]=student.getString("email");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "Something not ok", Toast.LENGTH_SHORT).show();
        }
    });    AppController.getInstance(getApplicationContext()).AddtoRequestQueue(request);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM