Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson反序列化JSON数组_Java_Json_Jackson - Fatal编程技术网

Java 使用Jackson反序列化JSON数组

Java 使用Jackson反序列化JSON数组,java,json,jackson,Java,Json,Jackson,我收到来自第三方服务提供商的JSON响应,其中包含一个对象数组。 当我尝试使用Jackson api反序列化JSON时。我得到了以下例外 com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of JacksonFields out of START_ARRAY token at [Source: java.io.BufferedReader@1015a9e; line: 5, col

我收到来自第三方服务提供商的JSON响应,其中包含一个对象数组。 当我尝试使用Jackson api反序列化JSON时。我得到了以下例外

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]
我的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"
            }
        ]
    }
}
我的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;
    }
}
我正在尝试使用以下代码反序列化JSON

ObjectMapper objmapper = new ObjectMapper();
objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JacksonJsonObj jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj.class);
如果我试着去做

JacksonJsonObj[] jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonJsonObj[].class);
它在BEGIN_对象本身失败

如何读取和反序列化JSON wiht数组。我应该写我自己的反序列化程序吗

编辑 如果我使用JSON字符串而不是流,那么我就能够取回所有Java对象。但为了更好的表现,我想让杰克逊继续工作

备用方式

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);
}
List JsonFieldsJackson=new ArrayList();
ObjectMapper mapper=新的ObjectMapper();
configure(在未知属性上反序列化feature.FAIL,false);
JsonNode nodes=mapper.readTree(strbuffer.toString());
nodes.elements();
迭代器iter=nodes.path(“结果”).path(“字段”).elements();
while(iter.hasNext()){
JsonNode=iter.next();
JsonFields fie=mapper.readValue(node.toString(),JsonFields.class);
JsonFieldsJackson.add(fie);
}

要反序列化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; 

}
然后您可以编写如下代码

JacksonFields jackobj = objmapper.readValue(new BufferedReader(new inputStreamReader(ipStream)), JacksonFields.class);
那就行了。
注意:-我没有为您可以提供的类提供正确的注释。

我认为您已经有2个JAR,即。 1。杰克逊核心 2.Jackson Mapper

因此,对于从JSON到POJO的解析

    ObjectMapper mapper = new ObjectMapper();
    JavaType javaType=mapper.getTypeFactory().constructType(JacksonFields.class);

    JacksonFields jksnflds = mapper.readValue(jsonString,javaType);
就这样