这是我想要获取数据的jSon数据文件,但有以下内容 -
error: 05-28 12:42:41.691: W/System.err(2887): org.json.JSONException: No value for
vehicle_type 05-28 12:42:41.691: W/System.err(2887): at
org.json.JSONObject.get(JSONObject.java:354)
05-28 12:42:41.691: W/System.err(2887): at
org.json.JSONObject.getString(JSONObject.java:510)
05-28 12:42:41.691: W/System.err(2887): at
com.example.capo.ui.MyCarActivity.returnjson(MyCarActivity.java:124)
05-28 12:42:41.691: W/System.err(2887): at
com.example.capo.net.JSONPostRequest.onPostExecute(JSONPostRequest.java:63)
05-28 12:42:41.691: W/System.err(2887): at
com.example.capo.net.JSONPostRequest.onPostExecute(JSONPostRequest.java:1)
JSON
代码:
{"status":"1","records":[{"vehicle_company":"jande","vehicle_type":"751","vehicle_color":"Black","vehicle_rc":"harry1234","vehicle_capacity":"6"}],"message":"selected
> Data"}
解析JSON
:
//...
// Parse String to JSON object
try {
jObj = new JSONObject(builder.toString());
// jarray = new JSONArray(builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
//...
要将对象映射到的POJO
Java
类:
公共课MyCar {
public String
vehicle_type,vehicle_company,vehicle_color,vehicle_rc,vehicle_capacity;
public String getVehicle_type() {
return vehicle_type;
}
public void setVehicle_type(String vehicle_type) {
this.vehicle_type = vehicle_type;
}
public String getVehicle_company() {
return vehicle_company;
}
public void setVehicle_company(String vehicle_company) {
this.vehicle_company = vehicle_company;
}
public String getVehicle_color() {
return vehicle_color;
}
public void setVehicle_color(String vehicle_color) {
this.vehicle_color = vehicle_color;
}
public String getVehicle_rc() {
return vehicle_rc;
}
public void setVehicle_rc(String vehicle_rc) {
this.vehicle_rc = vehicle_rc;
}
public String getVehicle_capacity() {
return vehicle_capacity;
}
public void setVehicle_capacity(String vehicle_capacity) {
this.vehicle_capacity = vehicle_capacity;
}
}
答案 0 :(得分:1)
转到JSONLint并解析它,您会看到JSON
在记录中的语法无效(缺少“”:“):
此致:
{
"status": "1",
"records[
{
"vehicle_company": "jande",
"vehicle_type": "751",
"vehicle_color": "Black",
"vehicle_rc": "harry1234",
"vehicle_capacity": "6"
}
],
"message": "selectedData"
}
正确:
{
"status": "1",
"records": [
{
"vehicle_company": "jande",
"vehicle_type": "751",
"vehicle_color": "Black",
"vehicle_rc": "harry1234",
"vehicle_capacity": "6"
}
],
"message": "selectedData"
}
答案 1 :(得分:0)
你的json在结果数组中是错的,它会像:
"result":
答案 2 :(得分:0)
您发布的json(我相信不是复制和粘贴错误)是错误的:
{"状态":" 1""的记录强> [{" vehicle_company":" jande& #34;"汽车类型":" 751"" vehicle_color":"黑色"" vehicle_rc":& #34; harry1234"," vehicle_capacity":" 6"}],"消息":"选择数据"}
应该(格式化):
{
"status":"1",
"records":[
{
"vehicle_company":"jande",
"vehicle_type":"751",
"vehicle_color":"Black",
"vehicle_rc":"harry1234",
"vehicle_capacity":"6"
}
],
"message":"selected Data"
}
答案 3 :(得分:0)
好的,我建议你转储数组中包含的对象"记录"然后父亲反对:
jObj = new JSONObject(builder.toString());
Log.v("JSON Parser", "jObj:"+jObj);
Log.v("JSON Parser", "array:"+jObj.getJSONArray("records"));
JSONObject elem = jObj.getJSONArray("records").getJSONObject(0);
Log.v("JSON Parser", "obj:"+elem);
看看会出现什么......