使用Gson从JSON对象获取键名

我有一个JSON对象,我想从中获取密钥名称并将它们存储在ArrayList中。 我使用了以下代码

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 entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); keys.add(key); i++; } nKeys = i; } 

如果我将此代码与简单的JSON对象一起使用

 { "age":100, "name":"mkyong.com", "messages":["msg 1","msg 2","msg 3"] } 

这很好用。 年龄,名称和消息(而不是值)将添加到我的ArrayList中。 一旦我尝试使用这个相同的代码与更复杂的JSON这样

 {"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;" } }} 

我只获得了根密钥。 有人能指出我正确的方向吗?

感谢大家!

我没有使用Gson(JSON)API来循环和条件化,而是更喜欢使用Gson来实现我认为最好的方法:只需几行代码即可提供非常简单的(de)序列化处理。 我将 (de)序列化问题与任何数据操作/查询/表示问题分离开来。 换句话说,尽可能合理,在序列化之前根据需要操作数据,并且类似地,在反序列化之后根据需要操纵数据。

所以,如果由于某种原因我想要JSON结构中的所有键,并且我想使用Gson,我可能会按如下方式处理它。 (当然,我现在无法想到为什么这样的事情会有用。)

 import java.io.FileReader; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import com.google.gson.Gson; public class App { public static void main(String[] args) throws Exception { List keys1 = getKeysFromJson("input_without_lists.json"); System.out.println(keys1.size()); System.out.println(keys1); List keys2 = getKeysFromJson("input_with_lists.json"); System.out.println(keys2.size()); System.out.println(keys2); } static List getKeysFromJson(String fileName) throws Exception { Object things = new Gson().fromJson(new FileReader(fileName), Object.class); List keys = new ArrayList(); collectAllTheKeys(keys, things); return keys; } static void collectAllTheKeys(List keys, Object o) { Collection values = null; if (o instanceof Map) { Map map = (Map) o; keys.addAll(map.keySet()); // collect keys at current level in hierarchy values = map.values(); } else if (o instanceof Collection) values = (Collection) o; else // nothing further to collect keys from return; for (Object value : values) collectAllTheKeys(keys, value); } } 

input_without_lists.json

 { "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;" } } } 

input_with_lists.json

 [{ "widget": { "debug": "on", "windows": [{ "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 },{ "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 },{ "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }] } }]