Json类型不匹配:无法转换为字符串

时间:2016-02-21 23:23:46

标签: java android json

我正在开发一个程序,该程序发布到服务器并检索json对象,然后将其转换为String数组。

当我在浏览器上测试结果时,我得到以下结果:

{"Bookname":["Alive-O","All Write Now ","Bun Go Barr 1","Planet Maths","Small World"],"SubjectName":["Religion","English","Irish","Maths","Science"]}

这是我发布和转换字符串的课程:

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://cosy.azurewebsites.net/bookrequest.php");

    User user = userLocalStore.getLoggedInUser();

    String username = user.username;

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair( "Username", username ));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();


    String result = EntityUtils.toString(entity);
    JSONObject jsonObject = new JSONObject(result);

    JSONArray jsonArray = jsonObject.getJSONArray("Bookname");
    BookName = new String[jsonArray.length()];
    for (int i = 0; i < jsonArray.length(); i++) {
        BookName[i] = jsonArray.getString(i);
    }

    // for getting subjectnames
    Log.v("we're getting a subject", "2");

    jsonArray = jsonObject.getJSONArray("SubjectName");
    BookSubject = new String[jsonArray.length()];
    for (int i = 0; i < jsonArray.length(); i++) {
        BookSubject[i] = jsonArray.getString(i);
    }

} 

以下是我的日志猫告诉我的事情:

02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:159)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:172)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at net.azurewebsites.cosy.Books$getBooks.doInBackground(Books.java:148)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at net.azurewebsites.cosy.Books$getBooks.doInBackground(Books.java:106)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-21 18:15:35.538    1436-3694/net.azurewebsites.cosy W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-21 18:15:35.542    1436-3694/net.azurewebsites.cosy W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-21 18:15:35.542    1436-3694/net.azurewebsites.cosy W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-21 18:15:35.542    1436-3694/net.azurewebsites.cosy W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-21 18:15:35.542    1436-3694/net.azurewebsites.cosy W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

我试图让它在课堂上用作数组。

1 个答案:

答案 0 :(得分:0)

错误说明您正在尝试将JSONArray转换为JSONObject,这意味着此说明:

JSONObject jsonObject = new JSONObject(result);

有问题。您获得的JSON很可能被括号括起来(因为您在浏览器中测试过,而不是在移动设备上测试)。尝试更改:

JSONObject jsonObject = new JSONObject(result);

JSONArray obj = new JSONArray(result);
JSONObject jsonObject = obj.getJSONObject(0);