Android - JSON No value for 1 - but there is using Log

时间:2017-08-04 12:15:25

标签: android json

I'm stuck at trying to get a simple string from a JsonObject. Here is the basic code. result is a JsonObject returned from an Asynctask. I've done dozens of the same way, and here it doesn't work.

Response:{"status":200}

Error: An error is thrown saying "No value for 1"

When I Log the content of the JsonObject, I get this: 200, which is what I want. For which reason could this return null when there is a value?

Edit if I use the result.has("status"); true is returned. I don't understand.

    Log.e("TAG", result.get("status").toString());
    try{
        String status = result.getString("status");
    } catch (Exception e){
         e.getMessage();
    }

1 个答案:

答案 0 :(得分:1)

You are getting int value in the object and you are holding in string which causes the error

You are getting int value in status

{ 
 "status":200
}

if it is like

{ 
 "status":"200"
}

Your code works perfectly.

Try this

    Log.e("TAG", result.get("status").toString());
    try{
        int status = result.getInt("status");
    } catch (Exception e){
         e.getMessage();
    }