简体   繁体   English

用Java解析Facebook FQL多查询

[英]Parsing Facebook FQL Multiquery in Java

I'm having some trouble with parsing an FQL multiquery for Facebook. 我在为Facebook解析FQL多查询时遇到麻烦。 I don't know how I need to handle the JSONArray with the same name in the result -> fql_result_set. 我不知道如何在结果-> fql_result_set中处理具有相同名称的JSONArray。 Here is the FQL JSON I got. 这是我得到的FQL JSON。

{
"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. 这是我目前在Java代码中得到的内容,但没有得到任何回报。 The data I get, will eventually go in an ArrayList. 我得到的数据最终将进入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? 有人知道我需要如何解析此JSON结果吗? 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" } ] **此时obj是"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. 根据循环计数, name具有值query1query2 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! 您可以使用到目前为止用于访问fql_result_set": [ { "uid": uid1, "eid": eid1, "rsvp_status": "attending" }, { "uid": uid2, "eid": eid2, "rsvp_status": "attending" }, { "uid": uid3, "eid": eid3, "rsvp_status": "attending" } ]的方法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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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