我从服务器获取了一个JSON数组。但我试图访问它我得到了未终止对象异常。
我检查输出是否有任何逗号错过或错误的语法但仍无法正常工作。
JSON数组
[
{
"cust_id": "8",
"mer_id": "3",
"fav": "1",
"cont_name": "sandeep",
"kirana_name": "kasar",
"phone_no": "9422879610",
"email_id": "kasar1@gmail.com",
"address": "nashik"
},
{
"cust_id": "8",
"mer_id": "1",
"fav": "1",
"cont_name": "sandeep",
"kirana_name": "general store",
"phone_no": "1212345623",
"email_id": "kasar@gmail.com",
"address": "nashik road nashik"
},
{
"cust_id": "8",
"mer_id": "5",
"fav": "0",
"cont_name": "a",
"kirana_name": "s",
"phone_no": "9422879610",
"email_id": "sam@gmail.com",
"address": "aa"
},
{
"cust_id": "8",
"mer_id": "6",
"fav": "0",
"cont_name": "see ya",
"kirana_name": "see ya kirana",
"phone_no": "8698766460",
"email_id": "deepmalasingh55@gmail.com",
"address": "dee"
},
{
"cust_id": "8",
"mer_id": "5",
"fav": "0",
"cont_name": "a",
"kirana_name": "s",
"phone_no": "9422879610",
"email_id": "sam@gmail.com",
"address": "aa"
},
{
"cust_id": "8",
"mer_id": "9",
"fav": "0",
"cont_name": "fre",
"kirana_name": "fee kirana",
"phone_no": "8698766460",
"email_id": "deepmalasinghop@gmail.com",
"address": "plotno:12,sahaydri nagar"
}
]
从数组中检索数据:
@Override
protected void onPostExecute(JSONObject response) {
super.onPostExecute(response);
progressDialog.dismiss();
try {
merchantsList.clear();
JSONArray jsonArray = response.getJSONArray("array");
if(jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("message")) {
String message = jsonObject.getString("message");
Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
snackbar.show();
} else {
long id,merchant_id,item_quantity;
String kirana_name,created_at,address;
int fav;
merchant_id=jsonObject.getLong("mer_id");
kirana_name=jsonObject.getString("kirana_name");
fav=jsonObject.getInt("fav");
address=jsonObject.getString("address");
Merchants merchants = new Merchants(merchant_id,kirana_name,address,fav);
merchantsList.add(merchants);
merchantsAdapter.notifyDataSetChanged();
}
}
}
例外:
org.json.JSONException: Unterminated object at character 973 of {"array":[{"cust_id":"8","mer_id":"3","fav":"1","cont_name":"sandeep","kirana_name":"kasar","phone_no":"9422879610","email_id":"kasar1@gmail.com","address":"nashik"},{"cust_id":"8","mer_id":"1","fav":"1","cont_name":"sandeep","kirana_name":"general store","phone_no":"1212345623","email_id":"kasar@gmail.com","address":"nashik road nashik"},{"cust_id":"8","mer_id":"5","fav":"0","cont_name":"a","kirana_name":"s","phone_no":"9422879610","email_id":"sam@gmail.com","address":"aa"},{"cust_id":"8","mer_id":"6","fav":"0","cont_name":"see ya","kirana_name":"see ya kirana","phone_no":"8698766460","email_id":"deepmalasingh55@gmail.com","address":"dee"},{"cust_id":"8","mer_id":"5","fav":"0","cont_name":"a","kirana_name":"s","phone_no":"9422879610","email_id":"sam@gmail.com","address":"aa"},{"cust_id":"8","mer_id":"9","fav":"0","cont_name":"fre","kirana_name":"fee kirana","phone_no":"8698766460","email_id":"deepmalasinghop@gmail.com","address":"plotno:12,sahaydri nagar"}]895da9a9a4cdd0825848479ee7e6c7ed2}
11-28 10:23:52.302 27004-27069/com.kiranaapp W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
出了什么问题?任何帮助都会很棒。直到你......
答案 0 :(得分:1)
是的,这是因为您的Json数组末尾包含无效字符串。 这是字符串。 的 895da9a9a4cdd0825848479ee7e6c7ed2 强> 删除它,然后重试。
修改强>
您可以自己检查。转到此站点Json Parser,然后将Json数组粘贴到那里。然后你会注意到一个错误。
答案 1 :(得分:0)
您的JSON中没有像"array"
这样的元素,您正在使用它。 JSON应该为每个值都有一些键。
JSONArray jsonArray = response.getJSONArray("array");
在这种情况下,您的JSON数组应该有"array"=[]