JSON解析失败android studio
Posted
技术标签:
【中文标题】JSON解析失败android studio【英文标题】:JSON Parsing fail android studio 【发布时间】:2015-10-19 21:43:47 【问题描述】:当我运行我的应用程序时,我遇到了这个异常。当我在我的第一个 json 解析应用程序中使用它时,我的代码运行正常。但是当我在我的新应用程序中实现我的代码时,我得到了这个异常。但其他代码工作正常。
07-29 11:48:38.269 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
07-29 11:48:38.269 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:96)
07-29 11:48:38.269 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:108)
07-29 11:48:38.269 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at com.fingerspot.mencobaagain.Product$LoadUsers.doInBackground(Product.java:89)
07-29 11:48:38.279 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at com.fingerspot.mencobaagain.Product$LoadUsers.doInBackground(Product.java:59)
07-29 11:48:38.289 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-29 11:48:38.289 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 14
07-29 11:48:38.289 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-29 11:48:38.289 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 15
07-29 11:48:38.299 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 16
07-29 11:48:38.299 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 17
07-29 11:48:38.299 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-29 11:48:38.299 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 18
07-29 11:48:38.299 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 19
07-29 11:48:38.299 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-29 11:48:38.299 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-29 11:48:38.309 4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 20
07-29 11:48:38.309 4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
这是代码
protected String doInBackground(String... args)
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(url, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Albums JSON: ", "> " + json);
try
users = new JSONArray(json);
if (users != null)
// looping through All albums
for (int i = 0; i < users.length(); i++)
JSONObject c = users.getJSONObject(i);
Toast.makeText(getBaseContext(),"errrrrrorrrrr",Toast.LENGTH_LONG).show();
// Storing each json item values in variable
String user_id = c.getString("user_id");
String name = c.getString("name");
String email = c.getString("email");
String display_name = c.getString("display_name");
String phone = c.getString("phone");
String ym = c.getString("ym");
String created_at = c.getString("created_at");
String last_login = c.getString("last_login");
String updated_at = c.getString("updated_at");
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
items.add(new ProductClass(Integer.parseInt(user_id), name, email, display_name,phone, ym, created_at, last_login, updated_at));
else
Log.d("Users: ", "null");
catch (JSONException e)
e.printStackTrace();
return null;
我预计获取 json 对象的代码有问题。
注意:我在旧应用和新应用上使用的 json 是相同的。
我的 json 对象
"DataUser":["user_id":1,"name":"Admin HO","email":"admin@kitasatu.net","display_name":null,"phone":null,"ym":null,"status":0,"location_id":0,"last_login":"2015-07-07 07:51:12","active":1,"created_at":"2015-05-02 00:00:00","updated_at":"2015-07-07 07:51:12"]
【问题讨论】:
***.com/questions/20653106/…的可能重复 【参考方案1】:您应该先将 json 字符串解析为 JSON 对象。
"DataUser":[
"user_id":1,
"name":"Admin HO",
"email":"admin@kitasatu.net",
"display_name":null,
"phone":null,
"ym":null,
"status":0,
"location_id":0,
"last_login":"2015-07-07 07:51:12",
"active":1,
"created_at":"2015-05-02 00:00:00",
"updated_at":"2015-07-07 07:51:12"
]
之后,您应该从之前解析的 JSON 对象中获取 json 数组。 “数据用户”
protected String doInBackground(String... args)
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(url, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Albums JSON: ", "> " + json);
JSONObject jsonObj = new JSONObject(json);
try
users = jsonObj.getJSONArray("DataUser");
if (users != null)
// looping through All albums
for (int i = 0; i < users.length(); i++)
JSONObject c = users.getJSONObject(i);
Toast.makeText(getBaseContext(),"errrrrrorrrrr",Toast.LENGTH_LONG).show();
// Storing each json item values in variable
String user_id = c.getString("user_id");
String name = c.getString("name");
String email = c.getString("email");
String display_name = c.getString("display_name");
String phone = c.getString("phone");
String ym = c.getString("ym");
String created_at = c.getString("created_at");
String last_login = c.getString("last_login");
String updated_at = c.getString("updated_at");
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
items.add(new ProductClass(Integer.parseInt(user_id), name, email, display_name,phone, ym, created_at, last_login, updated_at));
else
Log.d("Users: ", "null");
catch (JSONException e)
e.printStackTrace();
return null;
【讨论】:
哦,谢谢你的回答。实际上我刚才自己解决了我的问题:D【参考方案2】:我知道这不是您问题的解决方案,但您应该使用 GSON 来解析您的 json 响应。由于许多开源项目都在使用 gson。由于 GSON 增加了代码可读性并将解析时间从传统项目缩短了 30%。以下教程将帮助您理解它。http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON
从GSon.jar下载Gson.jar
如果您在创建 gson 模型时遇到问题,您可以通过Gson models online在线创建它们
【讨论】:
以上是关于JSON解析失败android studio的主要内容,如果未能解决你的问题,请参考以下文章