-4

I am getting a json object from the api, I want to parse this json object and get the text value. I am using volley library and need to load it in listiew adapter.The json is object-array-object type.This is my Json object. Can you please help.

{"result":[{"id":"1","ExpendName":"rice","Cost":"500","Dates":"2018-01-09 11:13:58"},{"id":"2","ExpendName":"Meat","Cost":"550","Dates":"2018-01-09 11:17:27"},{"id":"3","ExpendName":"Fish","Cost":"250","Dates":"2018-01-09 11:21:30"},{"id":"4","ExpendName":"Pant","Cost":"700","Dates":"2018-01-09 11:36:45"},{"id":"5","ExpendName":"Shirt","Cost":"1000","Dates":"2018-01-09 11:50:11"},{"id":"6","ExpendName":"Tea","Cost":"55","Dates":"2018-01-09 13:37:42"},{"id":"7","ExpendName":"Lunch, Tea, Transport ","Cost":"750","Dates":"2018-01-09 13:41:29"},{"id":"8","ExpendName":"Breakfast ","Cost":"30","Dates":"2018-01-10 05:34:07"},{"id":"9","ExpendName":"Train","Cost":"460","Dates":"2018-01-11 05:32:42"},{"id":"10","ExpendName":"Bus","Cost":"1250","Dates":"2018-01-11 05:33:11"},{"id":"11","ExpendName":"Train","Cost":"500","Dates":"2018-01-11 10:03:00"}]}
Taufiq
  • 23
  • 1
  • 5
  • 1
    Well, have you considered researching before posting this question? – Pulak Jan 12 '18 at 06:39
  • I have searced and tried a lot. but there is problem using jsonobject in jsonobject request. That's why I am seeking help now. – Taufiq Jan 12 '18 at 06:48

1 Answers1

0

You would normally parse the data this way:

try {
    JSONObject jsonObject = new JSONObject(rawData);
    JSONArray jsonArray = jsonObject.getJSONArray("result");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject mealObject = jsonArray.getJSONObject(i);
        String id = mealObject.getString("id");
        String expendName = mealObject.getString("ExpendName");
        String cost = mealObject.getString("Cost");
        String date = mealObject.getString("Dates");

        //Do whatever you want with the data
     }
 } catch (JSONException e) {
    e.printStackTrace();
 }
Pulak
  • 768
  • 7
  • 16
  • Here "rawData" is it the "response" of Json object? – Taufiq Jan 12 '18 at 08:34
  • It's the JSON data that you posted above. `rawData` is the `String` object storing the above JSON response. – Pulak Jan 12 '18 at 08:54
  • See the answers [here](https://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) for more information on parsing JSON – Pulak Jan 12 '18 at 08:56
  • It shows error in this line as I requested json object request `JSONObject jsonObject = new JSONObject(response);` Can you please help!! – Taufiq Jan 12 '18 at 10:38
  • `final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {@Overridepublic void onResponse(JSONObject response)` – Taufiq Jan 12 '18 at 10:51
  • Here `response` is a `JSONObject`. You can do `JSONObject jsonObject = response;` in the above code and everything else remain be same. – Pulak Jan 12 '18 at 10:54
  • It is showing error, Would you check it. `try { JSONObject jsonObject = response; JSONArray jsonArray = jsonObject.getJSONArray("result"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject mealObject = jsonArray.getJSONObject(i); Dataset dataSet = new Dataset(); dataSet.setName(mealObject.getString("ExpendName")); dataSet.setWorth(mealObject.getInt("Cost")); list.add(dataSet); }` – Taufiq Jan 12 '18 at 11:00
  • Yes, the code should be like this along with the `catch` block – Pulak Jan 12 '18 at 11:03
  • How do you expect help when you are not clear about the error message you are getting? It's not a site having magicians – Pulak Jan 12 '18 at 11:09