简体   繁体   中英

Parsing Facebook FQL Multiquery in Java

I'm having some trouble with parsing an FQL multiquery for Facebook. I don't know how I need to handle the JSONArray with the same name in the result -> fql_result_set. Here is the FQL JSON I got.

{
"data": [
{
  "name": "query1", 
  "fql_result_set": [
    {
      "uid": uid1, 
      "eid": eid1, 
      "rsvp_status": "attending"
    }, 
    {
      "uid": uid2, 
      "eid": eid2, 
      "rsvp_status": "attending"
    }, 
    {
      "uid": uid3, 
      "eid": eid3, 
      "rsvp_status": "attending"
    }
  ]
}, 
{
  "name": "query2", 
  "fql_result_set": [
    {
      "uid": uid1, 
      "name": "name1", 
      "pic_square": "pic1"
    }, 
    {
      "uid": uid2, 
      "name": "name2", 
      "pic_square": "pic2"
    }, 
    {
      "uid": uid3, 
      "name": "name3", 
      "pic_square": "pic3"
    }
  ]
}
]
}

Here is what I got at the moment in my Java code, but I get nothing in return. The data I get, will eventually go in an ArrayList.

try {
            GraphObject go = response.getGraphObject();
            JSONObject jso = go.getInnerJSONObject();
            JSONArray data = jso.getJSONArray("data");
            for(int i = 0; i < data.length(); i++){
                JSONObject o1 = data.getJSONObject(i);
                JSONArray rs1 = o1.getJSONArray("fql_result_set");
                for(int j = 0; j < rs1.length(); j++){
                    JSONObject rso1 = rs1.getJSONObject(j);
                    String uid = rso1.getString("uid");
                    String eid = rso1.getString("eid");
                    String rsvp = rso1.getString("rsvp_status");
                }
                JSONObject o2 = data.getJSONObject(i);
                JSONArray rs2 = o2.getJSONArray("fql_result_set");
                for(int k = 0; k < rs2.length(); k++){
                    JSONObject rso2 = rs2.getJSONObject(k);
                    String name = rso2.getString("name");
                    String pic = rso2.getString("pic_square");
                }

            }
} catch (JSONException e) {
            e.printStackTrace();
        }

Does anyone know how I need to parse this JSON result? Thanks in advance!

Solution

try {
            people = new ArrayList<Person>();
            GraphObject go = response.getGraphObject();
            JSONObject jso = go.getInnerJSONObject();
            JSONArray data = jso.getJSONArray("data").getJSONObject(0)
                    .getJSONArray("fql_result_set");
            JSONArray data2 = jso.getJSONArray("data").getJSONObject(1)
                    .getJSONArray("fql_result_set");


            for (int i = 0; i < data.length(); i++) {
                Person p = new Person();
                JSONObject o1 = data.getJSONObject(i);
                uid = o1.getString("uid");
                p.setUserId(uid);
                p.setRsvp_status(o1.getString("rsvp_status"));


                for (int j = 0; j < data2.length(); j++) {
                    JSONObject o2 = data2.getJSONObject(j);
                    if (p.getUserId().equals(o2
                            .getString("uid"))) {
                        p.setName(o2.getString("name"));
                        p.setPic(o2.getString("pic_square"));
                    }
                }
                people.add(p);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
// assign the whole result to a JSON Object
JSONObject result = whatever-variable-name

// get 'data' JSONArray
JSONArray array = result.getJSONArray('data')

// loop
for(i = 0; i < array.length(); i++) {
 JSONObject obj = array.getJSONObject(i);
 **
 name = obj.getString('name');
}

** at this point obj is "name": "query1", "fql_result_set": [ { "uid": uid1, "eid": eid1, "rsvp_status": "attending" }, { "uid": uid2, "eid": eid2, "rsvp_status": "attending" }, { "uid": uid3, "eid": eid3, "rsvp_status": "attending" } ]

and name has the value query1 or query2 depending on the loop count. You could you the methods used so far to gain access to fql_result_set": [ { "uid": uid1, "eid": eid1, "rsvp_status": "attending" }, { "uid": uid2, "eid": eid2, "rsvp_status": "attending" }, { "uid": uid3, "eid": eid3, "rsvp_status": "attending" } ] GLHF!

Try this:

 String strEvents = new JSONArray(apiResponse).getJSONObject(0).toString();
 String strVenues= new JSONArray(apiResponse).getJSONObject(1).toString();
 jsonArray  = new JSONObject(strEvents).getJSONArray("fql_result_set");
 jsonVenues = new JSONObject(strVenues).getJSONArray("fql_result_set");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM