【问题标题】:Second JSONArray in JSONObject somehow is emptyJSONObject 中的第二个 JSONArray 不知何故为空
【发布时间】: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


【解决方案1】:

您必须将您的 json 传递到一个数组中才能将它们全部提取出来。

应该是:

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

Related answer

或者你可以使用以下

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

【讨论】:

  • 是的,这将产生一个有效的 JsonArray。一个更好的方法是来自@sam-chaudhari 的答案。现在您必须将代码从解析更改为 JsonObject t JsonArray,带有 2 个对象,然后您就可以使用您的代码了。
【解决方案2】:

替换

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

有了这个

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

【讨论】:

    【解决方案3】:

    您的响应 JSON 无效,因为它缺少“,” 尝试更改您的回复来自

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

    常见错误

    • 期待 'STRING' - 您可能在结尾处多了一个逗号
      你的收藏。像 { "a": "b", }
    • 期望 'STRING'、'NUMBER'、'NULL'、'TRUE'、'FALSE'、'{'、'[' - 您的列表末尾可能有一个额外的逗号。 类似于:["a", "b", ]

    • 用引号将您的集合键括起来。 a
      的正确格式 集合是 { "key": "value" }

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2011-11-03
      • 2018-01-07
      • 2016-06-05
      • 1970-01-01
      相关资源
      最近更新 更多