Jon Jon - 1 year ago 97
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": "", "profile_picture": "", "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 Source

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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download