Java 使用Gson从JSON对象获取密钥名
我有一个JSON对象,我想从中获取密钥名并将其存储在ArrayList中。我使用了以下代码Java 使用Gson从JSON对象获取密钥名,java,json,parsing,key,gson,Java,Json,Parsing,Key,Gson,我有一个JSON对象,我想从中获取密钥名并将其存储在ArrayList中。我使用了以下代码 jsonData(String filename) { JsonParser parser = new JsonParser(); JsonElement jsonElement = null; try { jsonElement = parser.parse(new FileReader(filename)); } catch (JsonIOExcept
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;
}
这个很好用。将年龄、姓名和消息(而不是值)添加到我的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来实现我认为它最好的功能,而不是用循环和条件遍历Gson(JSON)API:只需几行代码就可以提供非常简单的(反)序列化处理。我将从(反)序列化问题中删除任何数据操作/查询/表示问题。换句话说,尽可能合理地在序列化数据之前根据需要操纵数据,同样地,在反序列化数据之后根据需要操纵数据 因此,如果出于某种原因,我希望所有的键都来自一个JSON结构,并且我希望使用Gson,那么我很可能会按如下方式进行处理。(当然,我现在想不出有什么理由可以让这样的东西有用。) 不带列表的输入。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;"
}
}
}
[{
"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
}]
}
}]
输入带有列表的列表。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;"
}
}
}
[{
"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
}]
}
}]
我认为您应该递归地迭代键,从哪个库中选择“JsonParser”、“JsonElement”和“JsonObject”?谢谢!我现在就试试看。我要做的是解析一个JSON对象,并将每个键的值放在一个数组中供以后使用。在我有了每个键之后,我想输出它们,然后让用户告诉我他们想要哪些数据。我找到的所有示例都显示了如何从键中提取数据,但您需要提前获取键。