I've the below data in a json file.
{
"product":"Data",
"data":{
"zip":[
{
"codeValue":"12345",
"distance":[
{
"5":"a c"
},
{
"2":"z c"
}
]
},
{
"codeValue":"60089",
"distance":[
{
"5":"a c"
},
{
"2":"z c"
}
]
}
]
}
}
and I'm trying to print the zip
size using the below code.
public void getJsonResponse() throws Exception {
String text = getTheData();
System.out.println(text);
ObjectMapper objMap = new ObjectMapper();
Map<String, Object> map = objMap.readValue(text, new TypeReference<Map<String, Object>>() {
});
String mainMap = map.get("product").toString();
String getData = map.get("data").toString();
Map<String, Object> map1 = objMap.readValue(getData, new TypeReference<Map<String, Object>>() {
});
List zip = (List) map1.get("zip");
System.out.println(zip.size());
}
here instead of sysout
on zip.size()
, if I used mainMap
, I get the output as Data, but when I use zip.size()
, I get some exception as
Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('z' (code 122)): was expecting double-quote to start field name at [Source: java.io.StringReader@60f82f98; line: 1, column: 3] at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1524) at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:557) at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:475) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddName(ReaderBasedJsonParser.java:1293) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._parseName(ReaderBasedJsonParser.java:1190) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:612) at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:412) at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:312) at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2105) at onlyJava.JsonDataCAll.getJsonResponse(JsonDataCAll.java:24) at onlyJava.Test.main(Test.java:13)
please let me know where have I gone wrong and how can I fix this.
3条答案
按热度按时间snz8szmq1#
这不会返回JSON,因此会出现异常。您将得到类似于{zip=[{...
而是将您的语句从
到
wgxvkvu92#
您进行了可以跳过的转换。请尝试以下操作:
w6lpcovy3#
强制转换为数组节点