-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": "[email protected]"
  },
  {
    "name": "zca",
    "id": "96",
    "email": "[email protected]",
  }
]

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 : [email protected]

Person 2 - Name : zca , id : 96 , email : [email protected]

Please show me the simplest way to do this

4
  • 1
    What have you tried so far? Please show your relevant code. Commented Mar 22, 2019 at 15:28
  • ok wait showing Commented Mar 22, 2019 at 15:29
  • Android has built in classes to handle JSON such as JSONArray and JSONObject. use them, then it is a simple matter of looping through the JSONarray Commented Mar 22, 2019 at 15:30
  • @Christopher please have a look. this method only get 1 row not the whole thing by Object o Commented Mar 22, 2019 at 15:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

[java updated] How can i do that ? if i use Object o , it only catches 1 row
show this bundle = new BundleFunctions().MakeBundleFromJSON(myResponse); bundle @NinjaStar
That's not right approach, you need to parse Json String to List<Person> i believe you are using json library @NinjaStar
no i am not using library, but which one should i use?
|
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.

2 Comments

can you show me an easy example please?
download GSon lib from google. Take the object created by Deadpool, you can find reference : tutorials.jenkov.com/java-json/gson.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.