gpt4 book ai didi

java - 使用 Gson 从 JSON 对象获取键名

转载 作者:行者123 更新时间:2023-12-03 20:24:22 27 4
gpt4 key购买 nike

我有一个 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<String, JsonElement> 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;"
}
}}

我只得到根 key 。有人可以为此指出正确的方向吗?

谢谢大家!

最佳答案

与其使用循环和条件遍历 Gson (JSON) API,我更愿意将 Gson 用于我认为它最擅长的方面:只需几行代码即可提供非常简单的(反)序列化处理。我会 decouple来自(反)序列化问题的任何数据操作/查询/表示问题。换句话说,尽可能合理地在序列化之前根据需要操作数据,同样地,在反序列化之后根据需要操作数据。

因此,如果出于某种原因我想要来自 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
}]
}
}]

关于java - 使用 Gson 从 JSON 对象获取键名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15731215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com