我有一个类似以下的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类可以解决问题,但显然不是我使用它的方式。
有人可以提出可能导致问题的原因或可能采用更好的方法吗?谢谢!
答案 0 :(得分:0)
dataObjects不是其他字符串的键。这是一个json对象。您必须单独将其解析为JSON对象。
答案 1 :(得分:0)
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();
}
}