Reputation: 23
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?
Upvotes: 0
Views: 170
Reputation: 134
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 {
}
}
Upvotes: 0
Reputation: 377
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();
}
Upvotes: 0
Reputation: 896
@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:
Upvotes: 0