MRP192
MRP192

Reputation: 7

I am getting HTTP response string like below. I want subject_id and status string from response string

Response string is like this:

{
  "images": [
    {
      "transaction": {
        "status": "success",
        "topLeftX": 325,
        "topLeftY": 451,
        "gallery_name": "Ironman",
        "subject_id": "Tony",
        "confidence": 0.99414,
        "height": 630,
        "width": 630,
        "face_id": 1,
        "quality": 1.75477
      },
      "candidates": [
        {
          "subject_id": "Tony",
          "confidence": 0.99414,
          "enrollment_timestamp": "1487644861022"
        },
        {
          "subject_id": "Tony",
          "confidence": 0.99414,
          "enrollment_timestamp": "1487644876280"
        }
      ]
    }
  ]
}

I tried this code but not working..

JSONArray arr = new JSONArray(response); 
JSONObject jObj = arr.getJSONObject(0); 
String status = jObj.getString("status"); 
String message = jObj.getString("subject_id");

Upvotes: 0

Views: 202

Answers (5)

Vugar Suleymanov
Vugar Suleymanov

Reputation: 317

You tried

JSONArray arr = new JSONArray(response);

but you should

JSONObject arr = new JSONObject(response);

Because your main json is json object, not JSONArray

Upvotes: 0

Basti Destruction
Basti Destruction

Reputation: 397

You can use GSON to parse JSON Strings. If you just want the first object of the images array you can use this code:

JsonParser jsonParser = new JsonParser();
JsonObject obj = jsonParser.parse(responseString).getAsJsonObject();
JsonArray images = obj.getAsJsonArray("images");
String subjectId = images.get(0).getAsJsonObject().get("transaction")
    .getAsJsonObject().get("subject_id").getAsString();

Upvotes: 0

Akash pasupathi
Akash pasupathi

Reputation: 304

use this

   @Override
    protected void onPostExecute(String result) {
        JSONObject jsonobj;
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        if (result != null) {
            if (result.equals("failure")) {
                Toast.makeText(context, "Check your Username or Password", Toast.LENGTH_LONG).show();
                dialog.dismiss();

            } else {//ths is getting data for vehicl_list_unread_count code, client id,restapi_key
                try {
                    Log.d(TAG, "onPostExecute: this inner of post" + getcontent_for_validate);
                    jsonobj = new JSONObject(getcontent_for_validate);
                    System.out.println("this is get content" + jsonobj.toString()); JSONArray array = jsonobj.getJSONArray("images");for (int i = 0; i < array.length(); i++) {
                       JSONArray transaction = array.getJSONObject(i).getJSONArray("transaction");for (int i = 0; i < array.length(); i++)
                        String status = transaction.getJSONObject(i).getString("status");
                        Password = editText_password.getText().toString();
                    }


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


            }


        } else {
            dialog.dismiss();
            Toast.makeText(context, "Check net connection", Toast.LENGTH_LONG).show();
        }

    }

Upvotes: 0

Vugar Suleymanov
Vugar Suleymanov

Reputation: 317

Use json simple lib

JSONObject json = new JSONObject(yourString);
JSONArray images = json.getJSONArray("images");

and you can loop throw this array

for (int i = 0; i < images.length(); i++) {
  JSONObject o = images.getJSONObject(i);
  ....
}

Upvotes: 1

Kaushal Patel
Kaushal Patel

Reputation: 338

You can create pojo class for response Also you can use GSON library for get response string.

Upvotes: 0

Related Questions