Jackson JsonMappingException: Can not deserialize instance

前端 未结 4 1592
一个人的身影
一个人的身影 2021-01-12 11:18

I\'m trying to convert my json data to a POJO object using Jackson. Here is the MainActivity and my POJO class code. I\'m basically getting a JsonMappingException error. I h

相关标签:
4条回答
  • 2021-01-12 11:33

    The structor of the json implies that you read it back in an object like this:

    class Response{
     private List<Entries> entries = ....
    public setEntries(List<Entrie> e)
    public getEntries()....
    
    }
    

    and load like this

     Response results = mapper.readValue(new URL("http://collegewires.com/android/jacksoncw.json"), Response.class);
    
    0 讨论(0)
  • 2021-01-12 11:36

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

    The key words here are ArrayList and START_OBJECT token. You cannot deserialize a single object into an array of objects. Try to make sense of doing that and you'll understand why.

    You can only deserialize an array of JSON objects into an array or collection of POJO's. Note that, whereas a JSON object is delimited by { } braces, an array is delimited by [ ] brackets, with some number of objects inside of it.

    0 讨论(0)
  • 2021-01-12 11:48
    1. Remove the Constructor in Entries & Phone

    2. GetEntries results = mapper.readValue(new URL("http://collegewires.com/android/jacksoncw.json"), GetEntries.class);

    3. Entries seems to be a parameter in your JSON.

    GetEntries.java

    package com.collegewires.jackson;
    
    import java.util.List;
    
    public class GetEntries{
        private List<Entries> entries;
    
        public List<Entries> getEntries(){
            return this.entries;
        }
        public void setEntries(List<Entries> entries){
            this.entries = entries;
        }
    }
    

    Entries.java

    package com.collegewires.jackson;
    
    import java.util.List;
    
    public class Entries{
        private String address;
        private String email;
        private String gender;
        private String id;
        private String name;
        private Phone phone;
    
        public String getAddress(){
            return this.address;
        }
        public void setAddress(String address){
            this.address = address;
        }
        public String getEmail(){
            return this.email;
        }
        public void setEmail(String email){
            this.email = email;
        }
        public String getGender(){
            return this.gender;
        }
        public void setGender(String gender){
            this.gender = gender;
        }
        public String getId(){
            return this.id;
        }
        public void setId(String id){
            this.id = id;
        }
        public String getName(){
            return this.name;
        }
        public void setName(String name){
            this.name = name;
        }
        public Phone getPhone(){
            return this.phone;
        }
        public void setPhone(Phone phone){
            this.phone = phone;
        }
    }
    

    Phone.java

    package com.collegewires.jackson;
    
    import java.util.List;
    
    public class Phone{
        private String home;
        private String mobile;
        private String office;
    
        public String getHome(){
            return this.home;
        }
        public void setHome(String home){
            this.home = home;
        }
        public String getMobile(){
            return this.mobile;
        }
        public void setMobile(String mobile){
            this.mobile = mobile;
        }
        public String getOffice(){
            return this.office;
        }
        public void setOffice(String office){
            this.office = office;
        }
    }
    
    0 讨论(0)
  • 2021-01-12 11:52

    Just put the data inside the [] rather than {} and it will work.

    For example:

    [
        {
            "id": "1",
            "name":"exemple"
        }
    ]
    
    0 讨论(0)
提交回复
热议问题