我正在获取HTTP响应字符串,如下所示。我想从响应字符串中获取subject_id和status字符串

时间:2017-03-04 18:35:49

标签: java android json

响应字符串是这样的:

{
  "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"
        }
      ]
    }
  ]
}

我试过这段代码但没有工作..

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

5 个答案:

答案 0 :(得分:1)

使用json simple lib

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

你可以循环抛出这个数组

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

答案 1 :(得分:0)

您可以为响应创建pojo类 您也可以使用GSON库获取响应字符串。

答案 2 :(得分:0)

使用此

   @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();
        }

    }

答案 3 :(得分:0)

您可以使用GSON解析JSON字符串。如果您只想要images数组的第一个对象,可以使用以下代码:

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();

答案 4 :(得分:0)

你试过

JSONObject arr = new JSONObject(response);

但你应该

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {

     //Try adapter.notifyDataSetChanged or something like passing the new(updated) data to adapter from here.        

    }
}

因为你的主json是json对象,而不是JSONArray