我有一个facebook图形响应,它获取了一组名称和 ID :
{"data":[{"id":"1402927803328937","name":"Spencer Jones"},{"id":"1402874766667901","name":"Jane Dickson"}],"}
我需要使用数组中的 ID 为第一个数组中返回的每个单独用户查询一次数据库,以检查该用户的“状态”。
我的代码目前正在查询数据库中两次相同的ID,而不是第一个数组中的两个不同ID 。我必须在错误的地方找到一些东西,但我无法理解。
06-13 21:08:54.081: D/FBID:(11514): 277976949048048
06-13 21:08:54.081: D/ID:(11514): 1402927803328937
06-13 21:08:54.081: D/NAME:(11514): Spencer Jones
06-13 21:08:54.631: E/JSON Parser(11514): Error parsing data org.json.JSONException: End of input at character 0 of
06-13 21:08:54.631: D/JSON 1:(11514): {"message":"Products found","success":1,"products":[{"from_user":"277976949048048","them_status":"accepted","status":"pending","to_user":"1402874766667901"}]}
06-13 21:08:54.631: D/FBID:(11514): 277976949048048
06-13 21:08:54.631: D/ID:(11514): 1402874766667901
06-13 21:08:54.631: D/NAME:(11514): Jane Dickson
06-13 21:08:55.191: E/JSON Parser(11514): Error parsing data org.json.JSONException: End of input at character 0 of
06-13 21:08:55.191: D/JSON 1:(11514): {"message":"Products found","success":1,"products":[{"from_user":"277976949048048","them_status":"accepted","status":"pending","to_user":"1402874766667901"}]}
日志中存在JSON解析错误,但它对我没有意义,因为它返回了我需要的所有4个变量。唯一的问题是它两次使用相同的ID,而不是两个不同的ID。
// getting JSON Array from facebook response
products = FriendResponse.getJSONArray("data");
// looping through products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fbid", fbid));
params.add(new BasicNameValuePair("id", id));
Log.d("FBID: ", fbid);
Log.d("ID: ", id);
// getting JSON string from URL
JSONObject json1 = jsonParser1.makeHttpRequest(url_friend_status, "POST", params1);
Log.d("JSON 1: ", json1.toString());
products1 = json1.getJSONArray(TAG_PRODUCTS);
// looping through products1
for (int j = 0; j < products1.length(); j++) {
JSONObject d = products1.getJSONObject(j);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String status = d.getString(TAG_STATUS);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}
}
答案 0 :(得分:0)
//从Facebook回复中获取JSON数组 products = FriendResponse.getJSONArray(&#34; data&#34;);
// looping through products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String status = c.getString(TAG_STATUS);
String id = c.getString(TAG_ID);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fbid", fbid));
params.add(new BasicNameValuePair("id", id));
Log.d("FBID: ", fbid);
Log.d("ID: ", id);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}
由于JSON文件只有一个数组(例如:{myJSONDATA:[{}]}),[]只是一个你不需要循环两次的数组,你应该只循环一次。