Extracting JSON fields using java

前端 未结 3 551
傲寒
傲寒 2021-01-15 12:55

I am trying to extract a person\'s details who liked a facebook page by passing the page id as parameter. I extracted the JSON content of that page and now from that I want

相关标签:
3条回答
  • 2021-01-15 13:37

    Code like this would do the trick.

    JSONObject json = readurl("https://graph.facebook.com/pageid");
    JSONArray dataJsonArray = json.getJSONArray("data");
    for(int i=0; i<dataJsonArray.length; i++) {
       JSONObject dataObj = dataJsonArray.get(i);
       String id = dataObj.getString("id");
       //Similarly you can extract for other fields.
    }
    

    Basically data is a JSONArray since it starts with [. So simply get would not work, you must use JSONArray.

    Note: I haven't compiled this code, but I think I gave you idea to proceed. Also refer this link to get hold of basics of parsing JSON in java.

    0 讨论(0)
  • 2021-01-15 13:46

    This snippet is not tested, but I'm pretty sure it works:

    JSONArray data = json.getJSONArray("data");
    for (int i=0; i < data.length(); i++) {
        JSONObject o = data.getJSONObject(i);
        sysout(o.getString("id");
        sysout(o.getString("name");
    }
    
    0 讨论(0)
  • 2021-01-15 13:46

    I use google's Gson library from: https://code.google.com/p/google-gson/

    0 讨论(0)
提交回复
热议问题