从Facebook图形响应解析java中的JSON数据

从Facebook图形响应解析java中的JSON数据,java,android,json,facebook,facebook-graph-api,Java,Android,Json,Facebook,Facebook Graph Api,这里我有一个来自facebook服务器的响应,它返回所选用户的相册列表。我想为所有标记为“name”的相册名称以及“link”和“cover_photo”创建一个数组,以便处理到listview中 { "data": [ { "id": "664462156031", "from": { "name": "Nate Ellender", "id": "68004222" }, "name": "Mobil

这里我有一个来自facebook服务器的响应,它返回所选用户的相册列表。我想为所有标记为“name”的相册名称以及“link”和“cover_photo”创建一个数组,以便处理到listview中

{
  "data": [
    {
      "id": "664462156031",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Mobile Uploads",
      "link": "https://www.facebook.com/album.php?fbid=664462156031&id=68004222&aid=2081375",
      "cover_photo": "754991180141",
      "count": 88,
      "type": "mobile",
      "created_time": "2012-05-12T00:38:14+0000",
      "updated_time": "2013-11-04T20:45:08+0000",
      "can_upload": false
    },
    {
      "id": "813288706431",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Bitstrips Photos",
      "link": "https://www.facebook.com/album.php?fbid=813288706431&id=68004222&aid=1073741826",
      "cover_photo": "813288781281",
      "count": 1,
      "type": "app",
      "created_time": "2013-10-31T21:53:11+0000",
      "updated_time": "2013-10-31T21:53:15+0000",
      "can_upload": false
    },
    {
      "id": "757384214481",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Galveston Vacation 2013",
      "description": "Our trip before joining Air Force",
      "link": "https://www.facebook.com/album.php?fbid=757384214481&id=68004222&aid=1073741825",
      "cover_photo": "757221350861",
      "count": 8,
      "type": "normal",
      "created_time": "2013-05-15T18:37:19+0000",
      "updated_time": "2013-05-15T22:12:52+0000",
      "can_upload": false,
      "likes": {
        "data": [
          {
            "id": "100002572634186",
            "name": "Misty O'Quain"
          },
          {
            "id": "100000582072776",
            "name": "Clifford Joyce"
          },
          {
            "id": "1045514613",
            "name": "Caity Ellender"
          }
        ],
        "paging": {
          "cursors": {
            "after": "MTA0NTUxNDYxMw==",
            "before": "MTAwMDAyNTcyNjM0MTg2"
          }
        }
      }
    },
    {
      "id": "542202136091",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Profile Pictures",
      "link": "https://www.facebook.com/album.php?fbid=542202136091&id=68004222&aid=2054735",
      "cover_photo": "749743202131",
      "count": 20,
      "type": "profile",
      "created_time": "2010-09-02T04:43:32+0000",
      "updated_time": "2013-04-13T13:46:14+0000",
      "can_upload": false
    },
    {
      "id": "646032913381",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Cover Photos",
      "link": "https://www.facebook.com/album.php?fbid=646032913381&id=68004222&aid=2079483",
      "cover_photo": "681058471881",
      "count": 2,
      "type": "cover",
      "created_time": "2012-03-22T00:25:50+0000",
      "updated_time": "2012-07-17T15:26:24+0000",
      "can_upload": false
    },
    {
      "id": "599115930391",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Timeline Photos",
      "link": "https://www.facebook.com/album.php?fbid=599115930391&id=68004222&aid=2073818",
      "cover_photo": "599115935381",
      "count": 1,
      "type": "wall",
      "created_time": "2011-10-20T14:25:23+0000",
      "updated_time": "2011-10-20T14:25:23+0000",
      "can_upload": false
    },
    {
      "id": "551798524851",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "My Year - 2010",
      "link": "https://www.facebook.com/album.php?fbid=551798524851&id=68004222&aid=2060899",
      "cover_photo": "551798544811",
      "count": 1,
      "type": "normal",
      "created_time": "2010-12-19T05:11:51+0000",
      "updated_time": "2010-12-19T05:12:14+0000",
      "can_upload": false
    },
    {
      "id": "522841060841",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "Night Lightnin",
      "description": "Always wanted to do this, because lightning is way cooler at night. Perhaps next time I'll be able to refine my methods a little. Read as \"bring tripod so you don't have to prop cam up on roof of car\"",
      "location": "Just north of Sulphur",
      "link": "https://www.facebook.com/album.php?fbid=522841060841&id=68004222&aid=2036447",
      "cover_photo": "522841195571",
      "count": 15,
      "type": "normal",
      "created_time": "2009-09-16T04:38:43+0000",
      "updated_time": "2009-09-16T04:42:32+0000",
      "can_upload": false
    },
    {
      "id": "513270939441",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "The Stuff That Doesn't Make Another Album",
      "description": "See title.",
      "location": "Good question",
      "link": "https://www.facebook.com/album.php?fbid=513270939441&id=68004222&aid=2029083",
      "cover_photo": "513271029261",
      "count": 9,
      "type": "normal",
      "created_time": "2009-03-03T06:43:09+0000",
      "updated_time": "2009-04-01T02:20:22+0000",
      "can_upload": false,
      "comments": {
        "data": [
          {
            "id": "513270939441_3444",
            "from": {
              "name": "Misty Bylsma Royal",
              "id": "592607110"
            },
            "message": "I love how the beautiful lady pile is in the same album as the bull pics. Nice.",
            "can_remove": false,
            "created_time": "2009-03-03T22:37:18+0000",
            "like_count": 0,
            "user_likes": false
          },
          {
            "id": "513270939441_3451",
            "from": {
              "name": "Nate Ellender",
              "id": "68004222"
            },
            "message": "Only because they are both things that i only had a few pictures of.",
            "can_remove": false,
            "created_time": "2009-03-04T02:46:11+0000",
            "like_count": 0,
            "user_likes": false
          }
        ],
        "paging": {
          "cursors": {
            "after": "Mg==",
            "before": "MQ=="
          }
        }
      }
    },
    {
      "id": "511605661671",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "What the...?",
      "description": "What's wrong with the rain? It's flaky and piles up on stuff... Did the plants leak something?\n",
      "location": "Outside",
      "link": "https://www.facebook.com/album.php?fbid=511605661671&id=68004222&aid=2026641",
      "cover_photo": "511605681631",
      "count": 54,
      "type": "normal",
      "created_time": "2008-12-11T11:59:27+0000",
      "updated_time": "2008-12-13T18:06:00+0000",
      "can_upload": false
    },
    {
      "id": "511565886381",
      "from": {
        "name": "Nate Ellender",
        "id": "68004222"
      },
      "name": "The Christmas Tree Farm",
      "description": "An Ellender Tradition",
      "location": "Grant",
      "link": "https://www.facebook.com/album.php?fbid=511565886381&id=68004222&aid=2026590",
      "cover_photo": "511566170811",
      "count": 19,
      "type": "normal",
      "created_time": "2008-12-09T02:38:54+0000",
      "updated_time": "2008-12-09T02:55:21+0000",
      "can_upload": false
    }
  ],
  "paging": {
    "cursors": {
      "after": "NTExNTY1ODg2Mzgx",
      "before": "NjY0NDYyMTU2MDMx"
    }
  }
}
此外,这里返回json数据(响应)。我需要开始解析响应下的数据

            Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
            Session.getActiveSession().requestNewReadPermissions(np);
            Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {

                }
            });
            rq.executeAsync();

任何例子都会有帮助。我想学习代码以更好地理解解析json数据。

您可以在这里获得一个很好的示例:

基本上,您必须使用
JSONObject
JSONArray
,您可以从
getJSONObject()
getJSONArray()
获得这些。 一旦找到合适的位置,使用
getJSONString()
获得所需的值

在您的情况下,它将是这样的:

JSONObject json = new JSONObject(response)
JSONArray jarray = json.getJSONArray("data");
for(int i = 0; i < jarray.length(); i++){
  JSONObject oneAlbum = jarray.getJSONObject(i);
  //get your values
  oneAlbum.getJSONString("name"); // this will return you the album's name.
}
JSONObject json=新的JSONObject(响应)
JSONArray jarray=json.getJSONArray(“数据”);
for(int i=0;i
希望这对你有帮助。

试试这个

            Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
            Session.getActiveSession().requestNewReadPermissions(np);
            Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {
                    JSONArray albumArr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");

                    for (int i = 0; i < albumArr.length(); i++) {
                        JSONObject item = albumArr.getJSONObject(i);
                        System.out.println("id : " + item.getString("id"));
                        System.out.println("fromName : " + item.getJSONObject("from").getString("name"));
                        System.out.println("fromid : " + item.getJSONObject("from").getString("id"));
                        System.out.println("link : " + item.getString("link"));
                        System.out.println("cover_photo : " + item.getString("cover_photo"));
                        System.out.println("count : " + item.getString("count"));
                        System.out.println("created_time : " + item.getString("created_time"));
                        System.out.println("updated_time : " + item.getString("updated_time"));
                        System.out.println("can_upload : " + item.getString("can_upload"));

                    }

                }
            });
            rq.executeAsync();
Session.NewPermissionsRequest np=newsession.NewPermissionsRequest(这是“朋友的照片”);
Session.getActiveSession().requestNewReadPermissions(np);
Request rq=新请求(Session.getActiveSession(),userID+“/albums”,null,HttpMethod.GET,new Request.Callback()){
@凌驾
未完成公共无效(响应){
JSONArray albumArr=response.getGraphObject().getInnerJSONObject().getJSONArray(“数据”);
对于(int i=0;i
这使用JSONObject json=jParser.getJSONFromUrl(url)获取json数据。如何在回调中识别要解析为响应的数据?可以使用
json=new-JSONObject(response)
where
response
是一个包含json的字符串。这正是我想要的,一个简短的示例。谢谢