gpt4 book ai didi

java - 使用 Gson 将 LinkedTreeMap 解码为属性?

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:12 28 4
gpt4 key购买 nike

我有一个如下所示的 JSON 结构:

{
"identifier": 1045608,
"scientificName": "Apis mellifera Linnaeus 1758",
"exemplar": false,
"richness_score": 400.0,
"dataObjects": [
{
"identifier": "d72801627bf4adf1a38d9c5f10cc767f",
"dataObjectVersionID": 30073527,
"dataType": "http://purl.org/dc/dcmitype/StillImage",
"dataSubtype": "",
"vettedStatus": "Trusted",
"dataRatings": {
"1": 0,
"2": 0,
"3": 4,
"4": 0,
"5": 6
},
"dataRating": 4.2,
"mimeType": "image/jpeg",
"created": "2009-07-12T15:13:19Z",
"title": "Honey Bee on Mountain Mint",
"language": "en",
"license": "http://creativecommons.org/licenses/by/2.0/",
"rightsHolder": "John Baker",
"source": "https://www.flickr.com/photos/38875278@N08/3730360050/",
"mediaURL": "https://farm3.staticflickr.com/2619/3730360050_c771a4c2cf_o.jpg",
"agents": [
{
"full_name": "John Baker",
"homepage": "http://www.flickr.com/photos/38875278@N08",
"role": "photographer"
},
{
"full_name": "Flickr: EOL Images",
"homepage": "http://www.flickr.com/groups/encyclopedia_of_life",
"role": "provider"
}
],
}
]
}

我定义了一个顶级类来反序列化为:

class EOLDataObjectsResponse {
private int identifier;
private String scientificName;
private Boolean exemplar;
@SerializedName("richness_score") private float richnessScore;
private List<EOLDataObjectsTaxonConcept> taxonConcepts;
private List<LinkedTreeMap<String, String>> dataObjects;
}

在我添加 dataObjects 属性之前,一切都可以使用 Gson 正确解析。我得到的测试结果是:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2242 path $.dataObjects[0].
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:825)
at emt.eol.EOLDataObjects.query(EOLDataObjects.java:102)
at emt.eol.EOLDataObjects.query(EOLDataObjects.java:51)
at emt.eol.EOLDataObjects.main(EOLDataObjects.java:136)

我想做的就是将该属性解析为字符串-字符串对的嵌套映射列表,因为我无法保证该列表中返回的内容,但希望在有人需要访问它时以嵌套映射格式提供它。我希望 Gson LinkedTreeMap 类能够解决这个问题,但显然不是我使用它的方式。

任何人都可以提出可能导致问题的原因或可能更好的方法吗?谢谢!

最佳答案

dataObjects 不像其他字符串那样是键。它是一个 json 对象。您必须将其单独解析为 JSON 对象。

    public static Map parse(final String json,final Map map){
try {

//Instance of JsonFactory for Object Mapper istance
final JsonFactory factory = new JsonFactory();

final ObjectMapper mapper = new ObjectMapper(factory);

//create JsonNode from json String
final JsonNode rootNode = mapper.readTree(json);

// iterate till it fetch all parameter and value from json string
final Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
while (fieldsIterator.hasNext()) {

final Map.Entry<String, JsonNode> field = fieldsIterator.next();

//if normal json, put value to map
map.put(field.getKey(), String.valueOf(field.getValue()));

//if json oject again recurse parse method.
if ((String.valueOf(field.getValue()).startsWith("{") && String.valueOf(field.getValue()).endsWith("}"))) {

parse(String.valueOf(field.getValue()),map);

}
//if json array it invoke parseJsonArray
if (String.valueOf(field.getValue()).startsWith("[{") && String.valueOf(field.getValue()).endsWith("}]")) {

parseJsonArray(String.valueOf(field.getValue()),map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}

public static void parseJsonArray(String jsonArray,Map<String, String> map) {
try {
JSONArray jsonArray1 = new JSONArray(jsonArray);
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject json = jsonArray1.getJSONObject(i);
Iterator<String> keys = json.keys();

while (keys.hasNext()) {
String key = keys.next();
map.put(key, String.valueOf(json.get(key)));
System.out.println("Key :" + key + " Value :" + String.valueOf(json.get(key)));
}

}
} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 使用 Gson 将 LinkedTreeMap 解码为属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220294/

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