Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从fql查询结果进行Android Json解析_Android_Json_Facebook_Facebook Fql - Fatal编程技术网

从fql查询结果进行Android Json解析

从fql查询结果进行Android Json解析,android,json,facebook,facebook-fql,Android,Json,Facebook,Facebook Fql,我正在为android开发Facebook应用程序。我正在使用Facebook流表。 它给了我正确的输出。但我无法解析返回的json字符串。 这是我的代码: void Query() { try { String fql = "SELECT attachment FROM stream WHERE post_id = '100008169074385_1408625132753109' "; Bundle parameters = new

我正在为android开发Facebook应用程序。我正在使用Facebook流表。 它给了我正确的输出。但我无法解析返回的json字符串。 这是我的代码:

void Query()
{
    try
    {
        String fql = "SELECT  attachment FROM   stream WHERE  post_id = '100008169074385_1408625132753109' ";

        Bundle parameters = new Bundle();

        parameters.putString("query", fql);
        parameters.putString("method", "fql.query");

        Session session = Session.getActiveSession();

        // Log.v("esty", session.getAccessToken());

        if (session != null && session.getState().isOpened()) {
            Log.v("esty", session.getAccessToken());
            Log.v("esty", session.getExpirationDate().toLocaleString());
        }
        parameters.putString("access_token", fb.getAccessToken());

        String response = null;    //hold my json String

        response = fb.request(parameters);  //Output JSON
        Log.v("esty2", response);
    }
    catch(Exception ex)
    {

        Log.v("esty2", ex.getMessage());
    }
}
输出josn为:

{
  "data": [
    {
      "attachment": {
        "media": [
          {
            "href": "", 
            "alt": "", 
            "type": "swf", 
            "src": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=87741124305&v=1&size=p&cksum=993c530ba81d9bc6b98190f1f4bbcb08&src=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FOVOngbevN2o%2Fhqdefault.jpg", 
            "swf": {
              "source_url": "http://www.youtube.com/v/OVOngbevN2o?list=PL6gx4Cwl9DGDiJSXfsJTASx9eMq_HlenQ&version=3&feature=share&autohide=1&autoplay=1&showinfo=1", 
              "preview_img": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=87741124305&v=1&size=p&cksum=993c530ba81d9bc6b98190f1f4bbcb08&src=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FOVOngbevN2o%2Fhqdefault.jpg", 
              "width": 0, 
              "height": 0, 
              "expanded_width": 0, 
              "expanded_height": 0
            }
          }
        ], 
        "name": "AJAX Tutorial - 19 - Creating Themes with CSS (+playlist)", 
        "href": "http://www.youtube.com/watch?v=OVOngbevN2o&feature=share&list=PL6gx4Cwl9DGDiJSXfsJTASx9eMq_HlenQ&index=18", 
        "caption": "Playlist by thenewboston", 
        "description": "Visit my website at http://thenewboston.com for all of my videos! \r\n\r\nMy Google+ - https://plus.google.com/108291790892450338168/posts\r\nFacebook - http://www.facebook.com/pages/TheNewBoston/464114846956315\r\nMy Twitter - http://twitter.com/#!/bucky_roberts\r\nMy Other YouTube Channel - http://www.youtube.com/thenewbostontv\r\nDonate - https://www.paypal.co...", 
        "properties": [
        ], 
        "icon": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t39.2081-0/10173489_10152389525269306_987289533_n.png"
      }
    }
  ]
}
我想要这个json的源url、预览img

如何解析这个json。请任何人帮帮我看看

JSONObject json=新的JSONObject(响应)

这将是您的整个
JSON
。然后你需要挖进去把剩下的挖出来

JSONArray data = json.getJSONArray("data");
JSONObject firstElement = data.getJSONObject(0);
JSONArray media = firstElement.getJSONObject("attachment").getJSONArray("media");
JSONObject anotherFirstElement = media.getJSONObject(0);
String source_url = anotherFirstElement.getJSONObject("sfw").getString("source_url");

要深入研究并查看发生了什么,您可以始终执行
System.out.println(json.toString(4))
查看每个元素中发生了什么。

它给出了一个例外:jsonarray无法转换为JSONObject。乍一看,没有注意到媒体嵌套在附件中。现在试试看,@this.esty