简体   繁体   中英

Java - How do I split the following string to get the information I need?

I'm using JSoup to scrape the Instagram API. I'm currently doing something simple: grabbing my own information.

When I query myself through the API it returns the following string:

{"meta": {"code": 200}, "data": {"username": "jon_perron", "bio": "George Brown", "website": "http://jonathanperron.ca", "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10865203_1593678807517862_1617189064_a.jpg", "full_name": "Jonathan Perron", "counts": {"media": 30, "followed_by": 51, "follows": 67}, "id": "1510848960"}}

I would like to split this string to remove all unnecessary data and to only keep the information I want. I'm looking to keep all information relating to my account. (username, bio, website, profile picture, full name and the counts)

How would one go about cleaning up this string and leave just that information behind?

而是使用JSON解析器库(例如Jackson )并将其转换为适当的POJO。

@Rorscharc advice is a good alternative. Just create classes with the attrs you need and convert it from Json to Java Objects using an external libs ( Jackson , Gson )

Examples: http://www.doublecloud.org/2015/03/gson-vs-jackson-which-to-use-for-json-in-java/

Comparison:

Jackson Vs. Gson

Try this

JSONParser parser = new JSONParser();
try {
    Object object = parser.parse(jsonString);
    JSONArray array = (JSONArray) object;

    JSONArray meta = (JSONArray) array.get("meta");
    JSONObject jsonObject = (JSONObject) meta.get("code");
} catch (ParseException e) {
    e.printStackTrace();
}

Try below code

APIs (Jackson 2 : core, data-bind, annotations)

public class Counts {

    private int media;

    private int followed_by;

    private int follows;

    @JsonProperty("media")
    public int getMedia() {
        return media;
    }

    public void setMedia(int media) {
        this.media = media;
    }

    @JsonProperty("followed_by")
    public int getFollowed_by() {
        return followed_by;
    }

    public void setFollowed_by(int followed_by) {
        this.followed_by = followed_by;
    }

    @JsonProperty("follows")
    public int getFollows() {
        return follows;
    }

    public void setFollows(int follows) {
        this.follows = follows;
    }

}

public class Data {

    private String username;
    private String bio;
    private String website;
    private String profile_picture;
    private String full_name;
    private Counts counts;
    private String id;

    @JsonProperty("username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonProperty("bio")
    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    @JsonProperty("website")
    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    @JsonProperty("profile_picture")
    public String getProfile_picture() {
        return profile_picture;
    }

    public void setProfile_picture(String profile_picture) {
        this.profile_picture = profile_picture;
    }

    @JsonProperty("full_name")
    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    @JsonProperty("counts")
    public Counts getCounts() {
        return counts;
    }

    public void setCounts(Counts counts) {
        this.counts = counts;
    }

    @JsonProperty("id") 
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

public class Meta {


    private int code;

    @JsonProperty("code")
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

}

public class User {

    private Meta meta;
    private Data data;

    @JsonProperty("meta")
    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    @JsonProperty("data")
    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

}

public class JsonParserClass {

    public Object parseJsonToObject(String jsonString, Class type)
        throws JsonParseException, JsonMappingException, IOException {

        ObjectMapper mapper = new ObjectMapper();

        Object object = mapper.readValue(jsonString, type);

        return object;
    }

}

public class Tester implements AutoCloseable{

    public static void main(String[] args) {

        try {
            String jsonString = (new Scanner(System.in)).nextLine();
            User user = (User) new JsonParserClass().parseJsonToObject(jsonString, User.class);
            System.out.println(user.getData().getFull_name());
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void close() throws Exception {

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM