Android how to get int type in JSONObject
you can do this ; when you try to create your json object , use this :
//method 1String myJson = " {errorCode:"+errorCode+"}";ErrorCode = new JSONObject(myJson);//method 2ErrorCode = new JSONObject();ErrorCode.put("errorCode", errorCode);// jsonObject.put(String key,Object value);
and then when you try to parse it , you can use :
json.getInt(String key); // in this case, the key is : errorCode
JSONObject
is the code equivalent to a json text-response. It's not a single key of that response. What you most likely want to do is read a single key thats contained in the servers response. For that, use JSONObject.getInt("key")
.
Sample:
int errorCode;JSONObject json = new JSONObject(serverResponseString);errorCode = json.getInt("error_key");
If you want to compose a JSONObject
instead of parsing one, use JSONObject.put()
.
int errorCode = 42;JSONObject json = new JSONObject();json.put("error_key", errorCode);String jsonString = json.toString();