Jon Jon - 2 months ago 19
Java Question

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?

Answer

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