我正在使用JSoup来抓取Instagram API。我目前正在做一些简单的事情:抓住我自己的信息。
当我通过API查询自己时,它会返回以下字符串:
{"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"}}
我想拆分此字符串以删除所有不必要的数据并仅保留我想要的信息。我希望保留与我的帐户相关的所有信息。 (用户名,生物,网站,个人资料图片,全名和计数)
如何清理这个字符串并将这些信息留在后面?
答案 0 :(得分:2)
答案 1 :(得分:0)
@Rorscharc建议是一个很好的选择。只需使用您需要的attrs创建类,并使用外部库(Jackson,Gson)将其从Json转换为Java对象
实施例: http://www.doublecloud.org/2015/03/gson-vs-jackson-which-to-use-for-json-in-java/
比较:
答案 2 :(得分:0)
试试这个
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();
}
答案 3 :(得分:0)
尝试以下代码
API(杰克逊2:核心,数据绑定,注释)
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 {
}
}