我有一个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)
我想做的是将这个属性解析成一个字符串-字符串对的嵌套Map列表,因为我不能保证返回到列表中的是什么,但如果有人需要访问它,我希望Gson LinkedTreeMap类能做到这一点,但显然不是我使用它的方式。
有谁能提出可能导致问题的原因或可能更好的方法吗?谢谢!
1条答案
按热度按时间lg40wkob1#
dataObjects不像其他字符串那样是一个key,它是一个json对象,你需要单独将它解析为JSON对象。