使用java提取JSON字段 [英] Extracting JSON fields using java
问题描述
我想通过将页面ID作为参数来提取一个喜欢Facebook页面的人的细节。我提取了该页面的JSON内容,现在我要提取用户的名称和ID。
I am trying to extract a person's details who liked a facebook page by passing the page id as parameter. I extracted the JSON content of that page and now from that I want to extract name and id of users.
如何实现?
JSONObject json = readurl("https://graph.facebook.com/pageid");
System.out.println(json.toString());
System.out.println("Page id is:"+json.get("id"));
JSON:
JSON:
"likes":{
"data":[
{
"id":"*******",
"name":"vv"
},
{
"id":"********",
"name":"abc"
},
推荐答案
这样的代码会做到这一点。 >
Code like this would do the trick.
JSONObject json = readurl("https://graph.facebook.com/pageid");
JSONArray dataJsonArray = json.getJSONArray("data");
for(int i=0; i<dataJsonArray.length; i++) {
JSONObject dataObj = dataJsonArray.get(i);
String id = dataObj.getString("id");
//Similarly you can extract for other fields.
}
基本上数据
一个JSONArray,因为它以 [
]开头。所以简单的 get
将无法正常工作,您必须使用JSONArray。
Basically data
is a JSONArray since it starts with [
. So simply get
would not work, you must use JSONArray.
注意:没有编译这段代码,但我想我给你的想法继续。另请参阅此链接,以取得在java中解析JSON的基础知识。
Note: I haven't compiled this code, but I think I gave you idea to proceed. Also refer this link to get hold of basics of parsing JSON in java.
这篇关于使用java提取JSON字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!