如何知道我的json值是boolean还是String值包含

时间:2017-06-20 06:42:22

标签: android json

在我的json中我得到一个键,其中有时它可能带有字符串值,或者可能是布尔值如何确定它来的值。

带字符串的JSON

{
  "status" : "success",
   "next" : "0.1"
}

与布尔

相同的JSON
{
  "status" : "success",
   "next" : false
}

我如何才能了解" next" key包含String或Boolean值?

1 个答案:

答案 0 :(得分:1)

请尝试这个

Object value = json.get("next");

if(value instanceof Boolean){
    boolean booValue = json.getBoolean("next");
}else {
    String str = json.getString("next");
}