1

I am writing an android code to parse a json array. I am not sure how it is to be done. I have however tried to parse the array.

These are the following things that I need to do:

I want to store the "status" in a variable.

Also, I want to store "ButtonImageUrl", "ImageUrl", "Title", "Url" iteratively in a string.

If anyone could guide me how should I proceed it would be very helpful.

{
  "first": {
    "message": "",
    "status": "Success"
  },
  "second": [
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    },
    {
      "ButtonImageUrl": "string1",
      "ImageUrl": "string2",
      "Title": "string3",
      "Url": "string4"
    }
  ]
}

Code:

private void jsonrequest2()
{

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

            try
            {

                jsonArray = null;
                jsonArray = response.getJSONArray("second");
                //jsonObject = response.getJSONArray("second");

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

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

                try
                {

                    ExampleItem exampleItem = new ExampleItem();

                    exampleItem.setmText1(jsonArray.getJSONObject(i).getString("Title"));
                    exampleItem.setmText2(jsonArray.getJSONObject(i).getString("Title"));
                    exampleItem.setImageUrl(jsonArray.getJSONObject(i).getString("ButtonImageUrl"));
                    //jsonArray.getJSONObject(i).getString("Title")

                    exampleList.add(exampleItem);

                }
                catch (JSONException e)
                {

                    e.printStackTrace();

                }

            }



            setuprecyclerview(exampleList);



        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });


    requestQueue = Volley.newRequestQueue(ProductPage1.this);
    requestQueue.add(request);


}
9
  • 1
    Use gson library to parson json to POJO github.com/google/gson Commented Sep 17, 2019 at 5:33
  • your response is not a JSONArray, it is a JSONObject Commented Sep 17, 2019 at 5:41
  • @VladyslavMatviienko Sir, can you please check if the updated code is correct or not. Commented Sep 17, 2019 at 6:45
  • Same incorrect. Your JSON is the JSON object, while you are doing a JsonArrayRequest Commented Sep 17, 2019 at 6:48
  • @VladyslavMatviienko Sir I have changed the JSONArray reponse to JSONObject reponse and changed the code accordingly. Sir can you please have a look at the code. Commented Sep 17, 2019 at 7:36

1 Answer 1

1

Firstly, the response is a JSONObject, not a JSONArray. Your code should look something like this.

public void onResponse(JSONObject response) {
    //Storing status into a variable
    try{
        JSONObject firstObject = response.getJSONObject("first");
        String status = firstObject.getString("status");

        //Accessing the JSONArray
        JSONArray jsonArray = response.getJSONArray("second");
        String mButtonImageUrl, mUrl, mImageUrl, mTitle;
        for(int i=0; i<jsonArray.length(); i++){
            JSONObject innerObject = jsonArray.getJSONObject(i);
            mButtonImageUrl = innerObject.getString("ButtonImageUrl");
            mUrl = innerObject.getString("Url");
            mImageUrl = innerObject.getString("ImageUrl");
            mTitle = innerObject.getString("Title");

            //These variables will be updated in every iteration. Store them or use them as you want here. 
        }
    }
    catch(JSONException e){
        e.printStackTrace();
    }
}
//...
Sign up to request clarification or add additional context in comments.

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.