I have a JSON object that I'd like to get the key names from and store them in an ArrayList. I've used the following code
jsonData(String filename) {
JsonParser parser = new JsonParser();
JsonElement jsonElement = null;
try {
jsonElement = parser.parse(new FileReader(filename));
} catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
int i = 0;
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String key = entry.getKey();
JsonElement value = entry.getValue();
keys.add(key);
i++;
}
nKeys = i;
}
If I use this code with a simple JSON object
{
"age":100,
"name":"mkyong.com",
"messages":["msg 1","msg 2","msg 3"]
}
This works fine. age, name, and messages (not the values) are added to my ArrayList. Once I try and Use this same code with a more complex JSON like this
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
I only get the root key. Can someone point me in the right direction with this?
Thanks everyone!
Instead of meandering through the Gson (JSON) API with loops and conditionals, I'd prefer to use Gson for what I think it does best: provide for very simple (de)serialization processing with just a few lines of code. I'd decouple any data manipulation/querying/presentation concerns from the (de)serialization concern. In other words, as much as reasonable, manipulate the data as necessary, before serializing it, and similarly, manipulate the data as necessary, after deserializing it.
So, if for some reason I wanted all of the keys from a JSON structure, and I wanted to use Gson, I'd likely approach it as follows. (Of course, I cannot currently think of any reason why such a thing could be useful.)
input_without_lists.json
input_with_lists.json