2015-12-03 77 views
-1

我目前有一個JSONObject,它包含不同類型的帶有布爾值的字符串。我如何獲得對象中的所有字符串和值?從JSONObject中檢索所有字符串和值

的JSONObject:

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

好了,問題是什麼? –

+0

@Simze如何獲取對象中的所有字符串和值。對不起,如果不是很清楚。 – pxtvr

回答

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 
} 
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 
+0

這相當有幫助!但是,我怎麼能爲對象中的所有字符串而不僅僅是一個字符串呢? – pxtvr

+0

看到編輯.. –

+0

非常感謝你! – pxtvr