- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在运行应用程序时收到这些错误。你能解释一下如何解决这个错误吗?这里 Logcat 输出
org.json.JSONException: Value [{"id":2,"username":"myusername","name":"myname","email":"myemail","password":"mypassword","groupid":1,"type":"mytype"},{"id":3,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"},{"id":4,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":5,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":6,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":7,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":8,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":9,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":10,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":11,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"1"},{"id":12,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":13,"username":"yuwah","name":"yu","email":"mail@gmail.com","password":"pass1","groupid":1,"type":"type1"},{"id":14,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"}] at listUsers of type java.lang.String cannot be converted to JSONArray
我的代码块是
public class MainActivity extends ListActivity {
// URL to get contacts JSON
private static String url = "http://staging.workberryplus.com/mobile/listUsers/1";
// JSON Node names
private static final String TAG_USERLIST = "listUsers";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Calling async task to get json
new GetUsers().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetUsers extends AsyncTask<Void, Void, Void> {
// Hashmap for ListView
ArrayList<HashMap<String, String>> userList;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
WebRequest webreq = new WebRequest();
// Making a request to url and getting response
String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET);
//Log.d("Response: ", "> " + jsonStr);
userList = ParseJSON(jsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, userList,
R.layout.list_users, new String[]{TAG_NAME, TAG_EMAIL
}, new int[]{R.id.name,
R.id.email});
setListAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseJSON(String json) {
if (json != null) {
try {
// Hashmap for ListView
ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();
JSONObject jsonObj = new JSONObject(json);
// Getting JSON Array node
JSONArray students = jsonObj.getJSONArray(TAG_USERLIST);
// looping through All Users
for (int i = 0; i < students.length(); i++) {
JSONObject c = students.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
// tmp hashmap for single user
HashMap<String, String> user = new HashMap<String, String>();
// adding each child node to HashMap key => value
user.put(TAG_ID, id);
user.put(TAG_NAME, name);
user.put(TAG_EMAIL, email);
//student.put(TAG_PHONE_MOBILE, mobile);
// adding users to users list
userList.add(user);
}
return userList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
return null;
}
}
}
我是安卓初学者。请帮助我如何解决这个错误。我正在谷歌搜索,但无法解决这个错误。对不起,我的英语不好。整个 logcat 输出为
> 03-31 04:12:01.468 3072-3250/com.nhn.userslist W/System.err: org.json.JSONException: Value [{"id":1,"username":"Nan Oo","name":"nanoo","email":"nann.oo@newhorizonnetworks.com","password":"nanoo","groupid":1,"type":"type1"},{"id":2,"username":"myusername","name":"myname","email":"myemail","password":"mypassword","groupid":1,"type":"mytype"},{"id":3,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"},{"id":4,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":5,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":6,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":7,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":8,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":9,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":10,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":11,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"1"},{"id":12,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":13,"username":"yuwah","name":"yu","email":"mail@gmail.com","password":"pass1","groupid":1,"type":"type1"},{"id":14,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"}] at listUsers of type java.lang.String cannot be converted to JSONArray
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:588)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at com.nhn.userslist.MainActivity.ParseJSON(MainActivity.java:103)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at com.nhn.userslist.MainActivity.access$200(MainActivity.java:19)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at com.nhn.userslist.MainActivity$GetUsers.doInBackground(MainActivity.java:69)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at com.nhn.userslist.MainActivity$GetUsers.doInBackground(MainActivity.java:42)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-31 04:12:01.469 3072-3250/com.nhn.userslist W/System.err: at java.lang.Thread.run(Thread.java:818)
03-31 04:12:01.873 3072-3072/com.nhn.userslist D/AndroidRuntime: Shutting down VM
03-31 04:12:01.873 3072-3072/com.nhn.userslist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nhn.userslist, PID: 3072
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.ListView.setAdapter(ListView.java:487)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at com.nhn.userslist.MainActivity$GetUsers.onPostExecute(MainActivity.java:89)
at com.nhn.userslist.MainActivity$GetUsers.onPostExecute(MainActivity.java:42)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-31 04:12:09.386 3072-3072/com.nhn.userslist I/Process: Sending signal. PID: 3072 SIG: 9
json 值为
[{
"id": 2,
"username": "myusername",
"name": "myname",
"email": "myemail",
"password": "mypassword",
"groupid": 1,
"type": "mytype"
}, {
"id": 3,
"username": "myusername",
"name": "myname",
"email": "myemail2",
"password": "mypassword2",
"groupid": 1,
"type": "mytype"
}, {
"id": 4,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 5,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 6,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 7,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 8,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 9,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 10,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 11,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "1"
}, {
"id": 12,
"username": "username1",
"name": "name1",
"email": "email1",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 13,
"username": "yuwah",
"name": "yu",
"email": "mail@gmail.com",
"password": "pass1",
"groupid": 1,
"type": "type1"
}, {
"id": 14,
"username": "myusername",
"name": "myname",
"email": "myemail2",
"password": "mypassword2",
"groupid": 1,
"type": "mytype"
}]
最佳答案
根据 JSON 响应 [array{object}] 方式,您将获取响应,并且直接使用对象而不获取数组
希望您能理解。
关于java - 字符串值无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327871/
我正在使用网络服务请求直方图数据。数据是数组中的一组数组: [[1375056000000,23.284713745117],[1375142400000,3.809531211853], [1375
这是我的 json 文件的相关部分: "categories": [ [ "Belgian Restaurant", "belgian" ],
我目前很困惑,为什么我不能在我的 Android 应用程序中从 JSONArray 中提取 JSONArray。下面给出了一个示例和我的源代码片段。 //The JSON { "cur
我正在尝试解析 JSONObject。 这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 我试图解析的 json 形式如下。 {
我正在尝试解析对象数据列表中的 JSON 数组 payment_details。 在这里,我已经成功解析了回收站 View 中调用的数据数组,但我无法调用 payment_details 数组。 适配
我正在寻找一个选项,可以将多个值添加到 JSONArray 并将其添加到另一个 JSONArray 中,而无需创建多个变量。例如: JSONArray array1 = new JSONArr
我遇到过一种情况,org.json.JSONArray 对象非常大,这最终会导致延迟和其他问题。因此,我们决定将 JSONArray 分割成更小的 block 。例如,如果 JSONArray 是这样
您好,我正在尝试在另一个 json 数组中解析一个 json 数组。帮我解决这个问题。 我的 Json 数组是 "results":[ { "percentcompleted"
我正在使用 Gson用于解析 json 响应。我需要在 JsonArray 中解析 JSONArry.. 我的回答是.. { "message": "Retreive sucessfully", "f
@GET @Produces("application/json") public Response GetAll() throws JSONException{ tblCategoryDao
我从服务器获取以下 json: { "Data": [ { "Record": [ " d11", "d12"
我正在使用 ajax 将 json 对象从 javascript 传递到 java servlet。 var jsonObj = JSON.stringify(objArray); //Then I
我需要获取作为“结果”内部数组的“示例”数据 JSON FILE 并将其附加到字符串。最简单的方法是什么? 这就是我得到“定义”的方式 private JSONObject queryResults;
我需要从 JSONArray 获取 JSONArray: JSONParser parser = new JSONParser(); JSONObject jObject=(J
我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。 这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。 [ "0x9000",
在此 Json 响应中,如何使用 Java 访问“smallImageUrls”并获取图像 url? 似乎“smallImageUrls”是“匹配”jsonarray 中的一个数组。如果我错了,请有人
这个问题在这里已经有了答案: Convert string to JSON array (10 个答案) 关闭 5 年前。 如何正确地将此字符串转换为 jsonArray? { "myArr
是否可以从较新的 EJB JSON 库转换为较旧的 org.json 库,而无需依赖 org.json.simple 或 GSON? 在下面的例子中,“buttons”是一个填充的 JsonArr
如何从jsonarray获取jsonarray? 我试过的代码。 for(loop) List stringList = new ArrayLis
我目前是 Json 新手,遇到了一个问题。我搜索了很多但找不到答案! 我从 json url 获取名称列表。名称可以在此 json 文件中重复,但我只想将它们的一条记录保留到我称为“arr”的新数组中
我是一名优秀的程序员,十分优秀!