string的值无法转换为JSONArray

时间:2016-03-31 08:21:36

标签: java android json

运行应用时出现这些错误。 你可以解释一下如何解决这个错误。 这里是Logcat out put

 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;
    }
}

}

我是android的初学者。请帮我解决这个错误。我在谷歌搜索,但我无法解决此错误。对不起,我的英语不好。 整个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"
}]

1 个答案:

答案 0 :(得分:1)

根据JSON响应[array {object}]方式,您获得响应,并且您直接使用对象而不获取数组

希望你明白。