【发布时间】:2018-01-27 03:58:31
【问题描述】:
这就是我想要做的。我想从我的 mysql 数据库中检索两个数组。这两个数组分别回显:
echo json_encode(array("friendrequest"=>$friendrequest));
echo json_encode(array("friendresult"=>$friendresult));
这是我用来处理结果字符串的代码:
public void onResponse(String response) {
Log.d("Response", response);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray ja_data = jsonObj.getJSONArray("friendresult");
String[] from = {
"first_name",
"anytimer_count",
"relationship_status"
}; //string array
int[] to = {
R.id.textListView1,
R.id.textListView2,
R.id.textListView3
}; //int array of views id's
JSONArrayAdapter arrayListAdapter = new JSONArrayAdapter(MyFriendsActivity.this, ja_data, R.layout.custom_list_items, from, to);
list.setAdapter(arrayListAdapter);
JSONArray ja_requests = jsonObj.getJSONArray("friendrequest");
int ja_lengths = ja_requests.length();
Log.d("Response", Integer.toString(ja_lengths));
} catch (JSONException e) {
Log.e("MyFriendsActivity", "unexpected JSON exception", e);
}
现在,处理代码中第 2 行记录的响应给了我以下信息:
响应 =
{
"friendrequest": [
{
"first_name": "Tom",
"anytimer_count": "0",
"relationship_status": "0"
},
{
"first_name": "Bert",
"anytimer_count": "0",
"relationship_status": "0"
}
]
}{
"friendresult": [
{
"first_name": "Luuk",
"anytimer_count": "0",
"relationship_status": "1"
}
]
}
这包含所有应该发送的值并且是正确的。
问题是我的处理代码只能识别在 php 脚本中首先编码的数组。基本上,如果我在 php 脚本中交换两行代码的位置,“friendrequest”将包含值,而“friendresult”则不包含值,反之亦然。我在这里做错了什么?
我得到的错误:
org.json.JSONException:friendresult 没有值
在 com.example.tomva.anytimereverywhere.MyFriendsActivity$3.onResponse(MyFriendsActivity.java:84)
第 84 行是以下行:
JSONArray ja_data = jsonObj.getJSONArray("friendresult");
【问题讨论】:
-
如果这是对您请求的完整响应,则它不是有效的 json。在两个对象之间缺少封闭
[ ]和,。比你的要求是给你一个带有 2 个对象的 JsonArray,.....
标签: java php android mysql json