有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Jackson将JSON解析为Java对象

头痛一周后,我来这里寻求你的帮助

我需要使用Jackson反序列化这个JSON输出

{
   "data": [
      {
         "id": "142065955831788",
         "name": "Name1",
         "link": "http://www.somelink.com",
      },
      {
         "id": "160212467350470",
         "name": "Name2",
         "link": "http://www.somelink2.com",
      }
      .
      .
      .
}

我发誓我什么都试过了,但我就是不能让杰克逊去序列化那个JSON

我错过了什么

编辑:

我创建了这样一个类:

数据。阶级

@JsonIgnoreProperties(ignoreUnknown = true)
public class Data{

    @JsonProperty("data")
    private String name;

    @JsonProperty("data")
    private String link;

//Getters + Setters

这是我的映射代码

ObjectMapper mObjectMapper = new ObjectMapper();

        ArrayList<Data> mDataList;

        mDataList = mObjectMapper.readValue(
                url, /* The url returning the JSON */
                mObjectMapper.getTypeFactory().constructCollectionType(
                        ArrayList.class, Data.class
                )
        );

我收到了这个错误信息:

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token


共 (1) 个答案

  1. # 1 楼答案

    这是代码-

    import org.codehaus.jackson.annotate.JsonIgnoreProperties;
    import org.codehaus.jackson.annotate.JsonProperty;
    import org.codehaus.jackson.map.ObjectMapper;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    class data {
    
        @JsonProperty("id")
        private String id ;
    
        @JsonProperty("name")
        private String name ;
    
        @JsonProperty("link")
        private String link;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getLink() {
            return link;
        }
    
        public void setLink(String link) {
            this.link = link;
        }
    
    }
    
    public class aa {
    
        public static void main(String[] args) {
            ObjectMapper mapper = new ObjectMapper();
    
            try {
    
                data[] d = mapper.readValue(
                                "[{\"id\":\"142065955831788\",\"name\":\"Name1\",\"link\":\"http://www.somelink.com\"},{\"id\":\"160212467350470\",\"name\":\"Name2\",\"link\":\"http://www.somelink2.com\"}]",
                                data[].class);
    
                for (data data1 : d) {
                    System.out.println("  -");
                    System.out.println("Id : "+data1.getId());
                    System.out.println("Name : "+data1.getName());
                    System.out.println("Link : "+data1.getLink());
                }
    
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            }
    
        }
    }
    

    您的JSON也需要更改。它不应该包含数据部分,因为您正在使用它的属性数组

    输出-

      -
    Id : 142065955831788
    Name : Name1
    Link : http://www.somelink.com
      -
    Id : 160212467350470
    Name : Name2
    Link : http://www.somelink2.com