从JSONObject中检索所有字符串和值

时间:2015-12-03 03:51:49

标签: java android

我目前有一个JSONObject,它包含不同类型的带有布尔值的字符串。如何在对象中获取所有字符串和值?

的JSONObject:

{ "1ed1":false, "1ed3":true, "1ep2":true }

2 个答案:

答案 0 :(得分:0)

JSONObject jObj = arr.getJSONObject(your object);
String led1 = jObj.getBoolean("led1");
String led2 = jObj.getBoolean("led2"); 
String led3 = jObj.getString("led3"); // if it is String otherwise the same as above

答案 1 :(得分:0)

如果您在JsonObject尝试以下解决方案中只有布尔值:

Iterator<?> keys = json.keys(); // json is your `JsonObject`
while( keys.hasNext() ) {
    String key = (String)keys.next();
    Log.e("key",""+key); // This will give you all the keys of your jsonObject, in your case it will be 1ed1,1ed3 etc...
    Log.e("value",""+json.getBoolean(key)); // This will give all the values associated with the key
}