0

I need to get one JSON value, but it is not working. I can already read the json data but I cannot add to the list and read just one or two values

 {
"item": [
    {
      "gallery": "user_files/goods/sOOsUcrM2Xorb3fbaD9bVoK9wDKDkmEjeKKZrJC7.jpeg, user_files/goods/Csc9XgxKUEkdI3jts7f2gV22hPqAKh5cJGYkvf7k.jpeg"

    }}

       JSONArray jsonArray = response.getJSONArray("result");
        for(int i = 0 ; i < jsonArray.length();i++)
        {
          JSONObject m = jsonArray.getJSONObject(i);
            String gallery = m.getString("gallery");
            urls.add(gallery);
        }
                                Toast.makeText(getActivity(),urls.get(0),Toast.LENGTH_LONG).show();

2 Answers2

1

First of all its a bad JSON format to read on Android or on any platform because coma(,) can never separate the JSON data... and still, if you want to use it then follow How to split a comma-separated string?

-1

As I Understand,you need one value only from multiple values.

you should try this.

JSONArray jsonArray = response.getJSONArray("result");
        for(int i = 0 ; i < jsonArray.length();i++)
        {
          JSONObject m = jsonArray.getJSONObject(i);
            String gallery = m.getString("gallery");
            String[] array=gallery.split(",");
            // array[0]; contains 1st value
            // array[1]; contains 2nd value
            urls.add(gallery);
        }
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40