-1

My app fetches JSON response from server. The response string has multiple rows and columns. I need to print this in java.

Here is my JSON response :

[
  {
    "name": "name2",
    "id": "99",
    "email": "ad@e.com"
  },
  {
    "name": "zca",
    "id": "96",
    "email": "as2c2@d.d",
  }
]

this is java part :

    OkHttpClient client = new OkHttpClient();

    String url = ServerConstants.BROWSE_URL;
    //String url = "https://reqres.in/api/users?page=2";

    Request request = new Request.Builder()
            .url(url)
            .build();

    client.newCall(request).enqueue(new Callback()
    {
        @Override
        public void onFailure(Call call, IOException e)
        {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException
        {
            if (response.isSuccessful())
            {
                final String myResponse = response.body().string();
                //System.out.println(Arrays.asList(new BundleFunctions().MakeArrayListFormJSON(myResponse)));
                bundle = new BundleFunctions().MakeBundleFromJSON(myResponse);
                //System.out.println("this is size ------- "+bundle.size());
                //System.out.println("this is response ------ "+myResponse);
                Browse.this.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        tv.setText(myResponse);

                        Set<String> keys = bundle.keySet();

                        for(String key : keys)
                        {
                            Object o = bundle.get(key);
                        }
                    }
                });
            }
        }
    });

I need a print on each person like this in java :

(Person number is according to array index FCFS)

Person 1 - Name : name2 , id : 99 , email : ad@e.com

Person 2 - Name : zca , id : 96 , email : as2c2@d.d

Please show me the simplest way to do this

NinjaStar
  • 1
  • 6

2 Answers2

1

JSON is really clear, which is Array of Objects in your case Person object, create Person POJO class

Person

public class Person {

 private String name;

 private String id;

 private String email;

 // getters and setters

 }

Parse the above json to List<Person> and print each Person as you like them

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
0

Your example of JSON doesn't looks multi dimensional array. It's an array with bunch of objects into it. If this is for java, why not use jackson or GSon or library like that to obtain object in an array list! All you have to define is a POJO object with matching variable names with getters and setters.

ringadingding
  • 475
  • 5
  • 14