如何从Json对象获取数据?
我正在与Facebook集成的Android应用程序。我正在使用fql查询从Facebook获取信息。我的fql方法是如何从Json对象获取数据?
void runfql(){
String fqlQuery = "SELECT uid, name, pic_small,birthday FROM user WHERE uid IN " +
"(SELECT uid2 FROM friend WHERE uid1 = me())";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
JSONObject myjson=response.getGraphObject().getInnerJSONObject();
Log.d("ResultResultResult: " ,""+myjson);
}
});
Request.executeBatchAsync(request);
}
myjson object has all info i want。像这样
{"data":[{"uid":536089174,"birthday":"July 22","name":"Usman Aslam Sheikh"},{"uid":581379174,"birthday":"July 26","name":"Ammar Khalid"}
问题是如何将这些信息存储到不同的数组?
请正确使用此代码。
String jsonString = yourstring;
JSONObject jsonResult = new JSONObject(jsonString);
JSONArray data = jsonResult.getJSONArray("data");
if(data != null) {
String[] names = new String[data.length()];
String[] birthdays = new String[data.length()];
for(int i = 0 ; i < data.length() ; i++) {
birthdays[i] = data.getString("birthday");
names[i] = data.getString("name");
}
}
检查http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
这是什么对象? '[{ “ID”: “1019”, “水平”: “1”, “time_in_secs”: “3”, “标准杆”: “0”, “首字母”: “EGB”, “引用”:“失败者!我是awesomer“,”time_stamp“:”2014-01-26 11:20:20“},{”id“:”1018“,”level“:”1“,”time_in_secs“:”3“,” par“:”0“,”首字母缩写“:”EGB“,”quote“:”你刚刚被一个女孩打败!“,”time_stamp“:”2014-01-26 11:19:24“},{” ID “:” 1032" , “水平”: “1”, “time_in_secs”: “3”, “平价”: “0”, “缩写”: “0”, “报价”: “输家”,“TIME_STAMP “:”2014-07-10 09:11:55“}]' – 2016-01-23 20:34:55
String jsonString = yourstring; JSONObject jsonResult = new JSONObject(jsonString); JSONArray data = jsonResult.getJSONArray(“”);如果(data!= null){ String [] names = new String [data.length()]; String [] birthdays = new String [data.length()];对于(int i = 0; i
编辑你的代码是这样的...这可能会帮助你..
JSONObject resultObject = new JSONObject(response);
JSONArray JArray = resultObject.getJSONArray("data");
for (int t=0; t<JArray.length(); t++) {
JSONObject JObject = JtArray.getJSONObject(t);
builder.append(JObject.getString("uid")+": ");
你找的JSONObject和JSONArray?或GSON也许? – BobTheBuilder 2013-04-10 06:57:55