2

I have been receiving JSON objects as a response from HttpsURLConnection. I have been using this to parse the response.

ObjectMapper map = new ObjectMapper();
JsonNode node = map.readTree(conn.getInputStream());

This has been working fine for me but now I am receiving arrays. How can I parse them?

This is an example of the response that I receive:

"value": [1 ] 0: { "ID": "2135125324" "name": "John" "2ndName": null "lastName": "James" }

1 Answers1

2

Please try this if you are using AsyncTask write the below code it would help you

private void yourfunction()
{

    class YourClass extends AsyncTask<String, Void, String>
    {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                JSONObject jsonObj = new JSONObject(s);
                user = jsonObj.getJSONArray("value");
                JSONObject c = user.getJSONObject(0);
                String profile = c.getString("ID");
                String name = c.getString("name");



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


        }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL("your url string");
                HttpURLConnection con= (HttpURLConnection) url.openConnection();
                bufferedReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
                String result;
                result = bufferedReader.readLine();
                return result;
            }
            catch (Exception e) {
                return null;
            }
        }
    }
    YourClass lu=new YourClass();
    lu.execute();
}
Jithin Kuriakose
  • 317
  • 5
  • 22