2016-09-22 49 views
0

我使用JSoup來刮取Instagram API。我現在正在做一些簡單的事情:抓住我自己的信息。Java - 如何分割下面的字符串以獲取我需要的信息?

當我查詢自己通過它返回以下字符串的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

試試這個

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(); 
} 
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 { 

    } 

}