【问题标题】:No value for uid error in JSON response on Android StudioAndroid Studio 上 JSON 响应中的 uid 错误没有值
【发布时间】:2017-09-28 16:41:45
【问题描述】:

在如何访问具有嵌套对象的“Json”对象方面需要一些帮助?尝试在 android studio 上读取 json 对象时,会出现此错误。已经检查了“uid”的 json 对象的结构,但仍然无法读取。因为错误显示“uid 没有值”。

05-01 07:42:16.478 11667-11667/com.hybridelements.medicalmanagement W/System.err:org.json.JSONException:uid 没有值

那么如何在对象内部获取正确的嵌套对象呢?

这是我在 android 中捕获的 json 对象。 :Json 对象:

{
  "0": {
    "patientName": "Hitamtestxvnccn",
    "uid": "5901e0fa972f63.91105545",
    "patient": {
      "careGiver": "www@www.com",
      "patientName": "Hitamtestxvnccn",
      "dob": "1945-03-21",
      "created_at": "2017-04-27 20:15:54",
      "updated_at": null
    }
  },
  "1": {
    "patientName": "qasdfghjm",
    "uid": "5901edbf5ebcc2.67869395",
    "patient": {
      "careGiver": "www@www.com",
      "patientName": "qasdfghjm",
      "dob": "1945-03-21",
      "created_at": "2017-04-27 21:10:23",
      "updated_at": null
    }
  },
  "2": {
    "patientName": "patientfromandroid",
    "uid": "590433fa0905a0.83840447",
    "patient": {
      "careGiver": "www@www.com",
      "patientName": "patientfromandroid",
      "dob": "1990-06-11",
      "created_at": "2017-04-29 14:34:34",
      "updated_at": null
    }
  },
  "3": {
    "patientName": "FromAndroid",
    "uid": "59043666cb9026.76887135",
    "patient": {
      "careGiver": "www@www.com",
      "patientName": "FromAndroid",
      "dob": "1995-06-11",
      "created_at": "2017-04-29 14:44:54",
      "updated_at": null
    }
  },
  "4": {
    "patientName": "wwwPatientAndroid",
    "uid": "590438b2eb99e7.18283913",
    "patient": {
      "careGiver": "www@www.com",
      "patientName": "wwwPatientAndroid",
      "dob": "1995-06-11",
      "created_at": "2017-04-29 14:54:42",
      "updated_at": null
    }
  },
  "error": false
}

用于读取 json 对象的代码。 :MainActivity.java:

try {
        JSONObject jObj = new JSONObject(response);
        boolean error = jObj.getBoolean("error");

        // Check for error node in json
        if (!error) {
            // Patient's details successfully captured

            // Now store the patients in SQLite
            for(int i = 0; i < jObj.length(); i++){
                String uid = jObj.getString("uid");

                JSONObject user = jObj.getJSONObject("id");
                String careGiver = user.getString("careGiver");
                String patientName = user.getString("patientName");
                String patientDOB = user.getString("dob");
                String created_at = user.getString("created_at");
                String updated_at = user.getString("updated_at");

                // Inserting row in users table
                db.addPatient(careGiver, patientName, uid, patientDOB, created_at, updated_at);
        }
    }

【问题讨论】:

  • 很多代码都被转储了。但是错误从何而来?缺什么?如果您有 JSON 异常,那么它不必对数据库做太多事情。插入也不行。那么你从哪里得到这个异常呢?
  • @greenapps 来自 android studio 日志,来自此特定行 'String uid = jObj.getString("uid");'在我的 MainActivity.java 中。
  • 错误很明显,你的json对象中没有uid。打印您的 json 对象以查看其中的实际内容。
  • 那么你就知道哪里出了问题。以及在哪里。你以前可以这么说的。现在找出缺少 uid 的原因。
  • 如您所见,我在android studio内部响应后提供了我的json对象,我已经多次检查并确保uid在json对象中,但仍然发生错误。

标签: php android mysql json sqlite


【解决方案1】:

因为您的“jObj”对象(主要是您的整个 Json 响应)没有“uid”作为键。此键位于您的 JSON 对象名称“1”、“2”等内。

【讨论】:

  • 感谢您的帮助和解释。非常感谢。顺便说一句,我刚刚在喝杯咖啡的同时解决了自己的问题。
【解决方案2】:

我终于通过一杯咖啡解决了我的问题。所以这是我的解决方案。在对 MainActivity.java 中的之前的 for 循环进行一些更改后,我将循环数从 for(int i = 0; i &lt; jObj.length(); i++) 更改为 for(int i = 0; i &lt; jObj.length()-1; i++)(来自 jObj.length() 的 -1)。然后我添加如下几行

String id = jObj.getString("uid);String id = jObj.getString(Integer.toString(i));

添加新行,如

JSONObject idd = jObj.getJSONObject(Integer.toString(i));

String uid = idd.getString("uid");

并将其添加到 for 循环中

JSONObject user = jObj.getJSONObject(Integer.toString(i)).getJSONObject("patient");

最后我设法将我的 Json 对象中的所有数据插入到数据库(Sqlite)中。

这是 for 循环方法的完整代码。

for(int i = 0; i < jObj.length()-1; i++){
                        String id = jObj.getString(Integer.toString(i));

                        JSONObject idd = jObj.getJSONObject(Integer.toString(i));
                        String uid = idd.getString("uid");


                        JSONObject user = jObj.getJSONObject(Integer.toString(i)).getJSONObject("patient");
                        String careGiver = user.getString("careGiver");
                        String patientName = user.getString("patientName");

                        String patientDOB = user.getString("dob");
                        String created_at = user.getString("created_at");
                        String updated_at = user.getString("updated_at");

                        // Inserting row in users table
                        db.addPatient(careGiver, patientName, uid, patientDOB, created_at, updated_at);
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多