JSONObject中的第二个JSONArray以某种方式为空

时间:2017-08-18 12:47:54

标签: java php android mysql json

这就是我想要做的事情。我想从我的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");

3 个答案:

答案 0 :(得分:2)

你必须在数组中传递你的json才能全部提取它们。

Shold be:

[
<?php
   echo json_encode(array("friendrequest"=>$friendrequest));
   echo ",";
   echo json_encode(array("friendresult"=>$friendresult));
?>
]

Related answer

或者您可以使用以下

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult));

答案 1 :(得分:1)

<强>替换

echo json_encode(array("friendrequest"=>$friendrequest));
echo json_encode(array("friendresult"=>$friendresult));

有了这个

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult));

答案 2 :(得分:0)

您的响应JSON无效,因为它已丢失&#34;,&#34; 尝试更改您的回复来自

{"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"}]}

[{
    "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"
    }]
}]

常见错误

  • 期待&#39; STRING&#39; - 你可能在结尾处有一个额外的逗号 你的收藏。像{&#34; a&#34;:&#34; b&#34;,}
  • 之类的东西
  • 期待&#39; STRING&#39;,&#39; NUMBER&#39;,&#39; NULL&#39;,&#39; TRUE&#39;,&#39; FALSE&#39 ,&#39; {&#39;,&#39; [&#39; - 您可能在列表末尾有一个额外的逗号。 类似的事情:[&#34; a&#34;,&#34; b&#34;,]

  • 将收藏集密封在引号中。正确的格式 集合是{&#34; key&#34;:&#34; value&#34; }

  • 确保您正确遵循JSON的语法。例如,总是 使用双引号,始终引用您的密钥,并删除所有 回调函数