【发布时间】: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