Java中的JSON到POJO类

时间:2017-04-11 10:23:35

标签: java json twitter

所以我有一个带有推文的文件。这样的事情。

{"text": "MVA/Transport. Greater Hume (Hume Hwy, Holbrook, NSW 2644) at 7 Mar 2017 03:58 #NSWRFS #MVATransport", "user": {"id": "4721717942", "name": "NSW Fire Updates"}, "lang": "en", "coordinates": { "coordinates": [147.273696, -35.785469] , "type":"Point"}, "created_at": "Mon Mar 06 17:29:31 +0000 2017"}

这是我创建的类tweet.java但我想将所有类放在tweet.java中。例如,coordinates和user应该在tweet.java里面

这就是我现在所拥有的。那么如何使坐标和用户成为推文的子类。

JsonInclude.Include.NON_NULL)

public class Tweet
{
    private String text;
    private String created_at;
    private User user;
    private Coordinates coordinates;

    public tweet();

    public tweet (String text, String created_at, User user, Coordinates coordinates);

    public String getText () {
        return text;
    }
    public void setText (String text) {
        this.text = text;
    }
    public String getCreated_at () {
        return created_at;
    }
    public void setCreated_at (String created_at) {
        this.created_at = created_at;
    }
    public User getUser () {
        return user;
    }
    public void setUser (User user) {
        this.user = user;
    }
    public Coordinates getCoordinates () {
        return coordinates;
    }
    public void setCoordinates (Coordinates coordinates) {
        this.coordinates = coordinates;
    }
    @Override
    public String toString() {
        return "ClassPojo [text = "+text+", created_at = "+created_at+", user = "+user+", coordinates = "+coordinates+"]";
    }
}


    public class Coordinates {
        private String type;
        private String[] coordinates;
        public String getType () {
            return type;
        }
        public void setType (String type) {
            this.type = type;
        }
        public String[] getCoordinates () {
            return coordinates;
        }
        public void setCoordinates (String[] coordinates) {
            this.coordinates = coordinates;
        }
        @Override
        public String toString() {
            return "ClassPojo [type = "+type+", coordinates = "+coordinates+"]";
        }
    }
    public class User {
        private String id;
        private String name;
        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;
        }
        @Override
        public String toString() {
            return "ClassPojo [id = "+id+", name = "+name+"]";
        }
    }

1 个答案:

答案 0 :(得分:0)

使用更快的xml的对象映射器。见here