[英]Deserializing a JSON array using Jackson
I am getting a JSON response from 3rd Party service provider and it has a array of objects in it. 我从第3方服务提供商处收到JSON响应,并且其中包含一系列对象。 When i am trying to deserialize JSON using Jackson api's. 当我尝试使用Jackson api反序列化JSON时。 I am getting following exception 我正在关注异常
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of JacksonFields out of START_ARRAY token
at [Source: java.io.BufferedReader@1015a9e; line: 5, column: 26]
My JSON response is 我的JSON响应是
{
"flags" : 1074200577,
"results" : {
"id1" : 0,
"id2" : 0,
"fields" : [
{
"id1" : 19202,
"id2" : 19202,
"count" : 0,
"format" : 8,
"type" : "name",
"flags" : 0,
"group" : 1074,
"value" : "1074"
},
{
"id1" : 19218,
"id2" : 19218,
"count" : 0,
"format" : 8,
"type" : "name",
"flags" : 0,
"group" : 1075,
"value" : "1075"
}
]
}
}
And my POJO class looks like this 我的POJO课看起来像这样
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
class JacksonFields {
int id1;
int id2;
int count;
int format;
String type;
int flags;
int group;
String value;
public JacksonFields(){
}
@JsonCreator
public JacksonFields(@JsonProperty("id1") int id1,
@JsonProperty("id2") int id2,
@JsonProperty("count") int count,
@JsonProperty("format") int format,
@JsonProperty("type") String type,
@JsonProperty("flags") int flags,
@JsonProperty("group") int group,
@JsonProperty("value") String value){
this.id1 = id1;
this.id2 = id2;
this.count = count;
this.format = format;
this.type = type;
this.flags = flags;
this.group = group;
this.value = value;
}
public void putId1(int id){
this.id1=id;
}
public void putId2(int id){
this.id2=id;
}
public void putCount(int count){
this.count=count;
}
public void putFormat(int format){
this.format=format;
}
public void putType(String type){
this.type=type;
}
public void putFlag(int flag){
this.flags=flag;
}
public void putGroup(int group){
this.group=group;
}
public void putValue(String val){
this.value=val;
}
}
class JacksonResults {
int id1;
int id2;
JacksonFields fields;
@JsonCreator
public JacksonResults(@JsonProperty("id1") int id1,
@JsonProperty("id2") int id2,
@JsonProperty("fields") JacksonFields fields){
this.id1 = id1;
this.id2 = id2;
this.fields = fields;
}
public JacksonResults(){
}
public void putId1(@JsonProperty("id1") int id){
this.id1 = id;
}
public void putId2(@JsonProperty("id2") int id){
this.id2 = id;
}
public void putFields(@JsonProperty("fields") JacksonFields fie){
this.fields = fie;
}
}
public class JacksonJsonObj{
Long flags;
JacksonResults res;
@JsonCreator
public JacksonJsonObj(@JsonProperty("flags") long flags,
@JsonProperty("results") JacksonResults res){
this.flags = flags;
this.res = res;
}
public JacksonJsonObj(){
}
public void putFlags(@JsonProperty("flags") long flag){
this.flags = flag;
}
public void putResults(@JsonProperty("results") JacksonResults res){
this.res=res;
}
}
I am trying to deserialize JSON using following code 我正在尝试使用以下代码反序列化JSON
ObjectMapper objmapper = new ObjectMapper();
objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JacksonJsonObj jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj.class);
if i try to do 如果我尝试去做
JacksonJsonObj[] jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj[].class);
it fails at the BEGIN_OBJECT itself. 它在BEGIN_OBJECT本身失败。
How to read and deserialize the JSON wiht Arrays. 如何使用数组读取和反序列化JSON。 Should i write my own deserializer? 我应该编写自己的解串器吗?
EDIT If i work on JSON String rather than stream i am able to get all Java objects back. 编辑如果我使用JSON String而不是Stream,我可以取回所有Java对象。 But for better performance i want Jackson to work on stream 但是为了获得更好的性能,我希望杰克逊能够继续工作
Alternate way 替代方式
List<JsonFields> JsonFieldsJackson = new ArrayList<JsonFields>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode nodes = mapper.readTree(strbuffer.toString());
nodes.elements();
Iterator<JsonNode> iter = nodes.path("results").path("fields").elements();
while(iter.hasNext()){
JsonNode node = iter.next();
JsonFields fie = mapper.readValue(node.toString(),JsonFields.class);
JsonFieldsJackson.add(fie);
}
I am considering that you already have 2 jars ie 1. Jackson Core 2. Jackson Mapper 我正在考虑您已经有2个罐子,即1.杰克逊核心2.杰克逊·马珀
So for Parsing from JSON to Your POJO 因此,从JSON解析到您的POJO
ObjectMapper mapper = new ObjectMapper();
JavaType javaType=mapper.getTypeFactory().constructType(JacksonFields.class);
JacksonFields jksnflds = mapper.readValue(jsonString,javaType);
and thats it !. 就是这样!
To deserialize the JSON you should have 3 class like 要反序列化JSON,您应该有3个类,例如
class Field{
int id1;
int id2;
int count;
int format;
String type;
int flags;
int group;
String value;
}
class Result{
int id1;
int id2;
Field[] fields;
}
class JacksonFields {
String flags;
Result result;
}
Then you can write code like 然后,您可以编写如下代码
JacksonFields jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonFields.class);
Then it will work. 然后它将起作用。 Note:-I did not provide proper annotation to the classes you can provide those. 注意:-我没有为可以提供的类提供适当的注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.