Java 如何解析来自FB graph API的JSON数组中的数据?
我是JSON新手。我在和警察一起工作。给定一个JSON,例如:Java 如何解析来自FB graph API的JSON数组中的数据?,java,json,processing,Java,Json,Processing,我是JSON新手。我在和警察一起工作。给定一个JSON,例如: { "data": [ { "name": "John Doe", "id": "2980311" }, { "name": "Jane Doe", "id": "10221412" }, { "name": "George Doe", "id": "11162
{
"data": [
{
"name": "John Doe",
"id": "2980311"
},
{
"name": "Jane Doe",
"id": "10221412"
},
{
"name": "George Doe",
"id": "111623489"
}
],
"paging": {
"blah" : "blah"
}
}
如何获取id值。我首先尝试使用一个数组:
String URL = "https://graph.facebook.com/username/friends?access_token=";
String token = "";
String[] response = loadStrings(URL+token);
if(response != null) {
JSONObject result = new JSONObject(response);
JSONArray data = result.getJSONArray("data");
}
这个打印出来:
JSONObject["data"] not found.
JSONObject["data"] is not a JSONArray.
但它肯定得到了数据。如果我打印出响应
我会看到数据
我遗漏了什么?我在该库中没有看到接受字符串[]
(您的响应类型
变量)的构造函数。有JSONObject(String)
和JSONObject(Object)
。如果您将字符串[]
传递到JSONObject
,它将匹配后者,在我看来,后者不会(立即)满足您的要求
我怀疑您想从URL中获取一个字符串
,然后将其传递到JSONObject(String)
,此时事情应该开始正常工作。尝试一下
GraphObject responseGraphObject=response.getGraphObject()JSONObject json=responseGraphObject.getInnerJSONObject()
@混音:好极了!这有点像是瞎猜,因为我还没有用过那个图书馆,很高兴这有帮助!
JSONObject sys = json.getJSONObject("data");
Log.e("urlimage json", sys.getString("url"));