Reputation: 4363
I've a JSONObject
that contains other objects:
{
"1" : {
"state": [],
"hue": 224
}
"2" : {
"state": [],
"hue": 224
}
"3" : {
"state": [],
"hue": 224
}
}
It's the response from the Philips Hue Brigde JSON that contains all the connected lights. I use Volley for the http connection. I created a Light class where I store the data like state, hue, bri etc. I fill the Light class by using of a for loop in the onResponse from Volley:
for (int i = 0; i <= response.length(); i++) {
JSONObject lightObject = response.getJSONObject(String.valueOf(i));
System.out.println("OBJECT " + lightObject);
// Use try/catch because of empty or unknown key
try {
description = lightObject.getString("type");
} catch (Exception e) {}
try {
bri = lightObjectState.getInt("bri");
} catch (Exception e) {}
try {
sat = lightObjectState.getInt("sat");
} catch (Exception e) {}
try {
hue = lightObjectState.getInt("hue");
} catch (Exception e) {}
Light light = new Light(String.valueOf(i),lightObject.getString("name"), description, on, bri, sat, hue);
mLightList.add(light);
mLightAdapter.notifyDataSetChanged();
}
The problem with the for loop is that, when the Light id starts with greater then zero, it will crash my Android app, because there is no value of 0
.
My question is how I can get the first object of the response without knowing the key
name that contains the object with all the properties of one light, so when the JSONObject
looks like below, the app will not crash, but just display light 4 till 6:
{
"4" : {
"state": [],
"hue": 224
}
"5" : {
"state": [],
"hue": 224
}
"6" : {
"state": [],
"hue": 224
}
}
If there are any questions left, please let me know. Thanks.
Upvotes: 2
Views: 3293
Reputation: 31015
// use keys() iterator, you don't need to know what keys are there/missing
Iterator<String> iter = response.keys();
while (iter.hasNext()) {
String key = iter.next();
JSONObject lightObject = response.getJSONObject(key);
System.out.println("key: " + key + ", OBJECT " + lightObject);
// you can check if the object has a key before you access it
if (lightObject.has("bri")) {
bri = lightObject.getInt("bri");
}
if (lightObject.has("sat")) {
sat = lightObject.getInt("sat");
}
if (lightObject.has("hue")) {
hue = lightObject.getInt("hue");
}
if (lightObject.has("name")) {
name = lightObject.getString("name")
}
Light light = new Light(key, name, description, on, bri, sat, hue);
mLightList.add(light);
}
mLightAdapter.notifyDataSetChanged();
Upvotes: 4
Reputation: 1802
try with object key:
Iterator<String> keys = jsonObject.keys();
if( keys.hasNext() ){
String key = (String)keys.next(); // First key in your json object
}
Upvotes: 2
Reputation: 2307
Let's say you have a class called Bulb
public class Bulb {
int[] state;
int hue;
}
Then you can use a JSON serializer like GSON
And then you can map your response to in a Map ( like HashMap ) like this
Type typeOfHashMap = new TypeToken<Map<String, Bulb>>() { }.getType();
Map<String,Bulb> bulbMap = gson.fromJson(json, typeOfHashMap);
And then you can iterate over the map with keys like so
Iterator bulbIterator = bulbMap.entrySet().iterator();
while (bulbIterator.hasNext()) {
Map.Entry pair = (Map.Entry)bulbIterator.next();
String pKey = (String) pair.getKey();
Bulb bulb = (Bulb) pair.getValue();
// Do something here with the bulb
}
Upvotes: 0