JSON类型不匹配或org.json.jsonecxeption

bxfogqkk  于 2022-12-01  发布在  其他
关注(0)|答案(9)|浏览(121)

链接为http://iipacademy.in/askpoll/ten_feed.php
异常位于onPostExecute()方法中(第4行):

Log.i("result", result);
try {
    if (result != null) {
        JSONArray jsonArray = new JSONArray(result); // erreor
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject objJson = jsonArray.getJSONObject(i);

            TopTenGetterSetter obj = new TopTenGetterSetter();  

            obj.setQ(objJson.getString("question"));
            obj.setA(objJson.getString("option1"));
            obj.setB(objJson.getString("option2"));
            obj.setC(objJson.getString("option3"));
            obj.setD(objJson.getString("option4"));

            polls.add(obj);
                        }

    }
} catch (JSONException e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "error",
            Toast.LENGTH_SHORT).show();
}

日志:

12-18 03:20:45.447: W/System.err(2790): org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSON.typeMismatch(JSON.java:111)
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.<init>(JSONArray.java:91)
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.<init>(JSONArray.java:103)
12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:188)
12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:1)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.finish(AsyncTask.java:631)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-18 03:20:45.447: W/System.err(2790):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 03:20:45.447: W/System.err(2790):     at android.os.Looper.loop(Looper.java:137)
12-18 03:20:45.447: W/System.err(2790):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invoke(Method.java:525)
12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 03:20:45.447: W/System.err(2790):     at dalvik.system.NativeStart.main(Native Method)
12-18 03:20:45.447: D/dalvikvm(2790): GC_FOR_ALLOC freed 5131K, 55% free 4437K/9672K, paused 2ms, total 2ms

消息是一个数组,那么它的代码应该是什么,或者如何求解?
提前感谢......

zkure5ic

zkure5ic1#

org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray

响应似乎是字符串而不是json数组

{  // json object node 
    "response": { // json object response
        "result": 1,
        "Message": [ // json array Message
            {        // json object node 
                "pollid": "98",
                 "category": "Entertainment",
                 "question": "what",  //  string
                 "option1": "981.mov",

结果是json对象而不是json数组

JSONArray jsonArray = new JSONArray(result);

应该是

JSONObject jObj = new JSONObject(result);
JSONObject response = jObj.getJSONObject("response");
//JSONObject jb = new JSONObject(response);
JSONArray jr = response.getJSONArray("Message");
for(int i=0;i<jr.length();i++)
{
JSONObject jb1 = jr.getJSONObject(i);
String question = jb1.getString("question");
Log.i(".......",question);
}
uajslkp6

uajslkp62#

结果不是数组,消息是。

izkcnapc

izkcnapc3#

试试这个:

Log.i("result", result);
try {
    if (result != null) {
        JSONObject jObject = new JSONObject(result);
        JSONArray jsonArray = jObject.getJSONObject("response").getJSONArray("Message");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject objJson = jsonArray.getJSONObject(i);

            TopTenGetterSetter obj = new TopTenGetterSetter();  
            /*
             * [{"job_id":"1","job_staff_id":"","job_name":"Account",
             * "job_detail":"test\r\ntesds","job_start_date":
             * "2013-11-08"
             * ,"job_end_date":"2013-11-10","job_amount":"500"
             * ,"job_progress":"","job_complete_status":"0"}]
             */

            obj.setQ(objJson.getString("question"));
            obj.setA(objJson.getString("option1"));
            obj.setB(objJson.getString("option2"));
            obj.setC(objJson.getString("option3"));
            obj.setD(objJson.getString("option4"));

            polls.add(obj);
            // am = Customer.info.get(pos).getJamount();

            // Toast.makeText(getApplicationContext(), am + result,
            // Toast.LENGTH_LONG).show();
        }

    }
} catch (JSONException e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "error",
            Toast.LENGTH_SHORT).show();
}

正如mario和nfear所说,您试图将一个JSONObject转换成一个JSONArray。

gijlo24d

gijlo24d4#

字符串内容JSONObject作为根元素,而不是JSONArray.to从String获取Message JSONArray,您应该首先获取response JSONObject,然后获取Message JSONArray,如下所示:

JSONObject jsonobj = new JSONObject(result);
 // get response JSONObject

 JSONObject jsonobj_response = jsonobj.getJSONObject("response");

// get Message JSONArray from jsonobj_response

 JSONArray jsonArray = jsonobj_response.getJSONArray("Message");
  // your code here.....
tcomlyy6

tcomlyy65#

试试这个

try {
    if (result != null) {
        JSONArray jsonArray = new JSONObject(result).getJSONObject("response").getJSONArray("Message");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject objJson = jsonArray.getJSONObject(i);

            TopTenGetterSetter obj = new TopTenGetterSetter();  
            /*
             * [{"job_id":"1","job_staff_id":"","job_name":"Account",
             * "job_detail":"test\r\ntesds","job_start_date":
             * "2013-11-08"
             * ,"job_end_date":"2013-11-10","job_amount":"500"
             * ,"job_progress":"","job_complete_status":"0"}]
             */

            obj.setQ(objJson.getString("question"));
            obj.setA(objJson.getString("option1"));
            obj.setB(objJson.getString("option2"));
            obj.setC(objJson.getString("option3"));
            obj.setD(objJson.getString("option4"));

            polls.add(obj);
            // am = Customer.info.get(pos).getJamount();

            // Toast.makeText(getApplicationContext(), am + result,
            // Toast.LENGTH_LONG).show();
        }

    }
}
lqfhib0f

lqfhib0f6#

您发布的JSON具有

// Key "response" , Value type JsonObject

 JSONObject jsonObject=jsonObjectOfRsponse.getJsonObject(key);

//Key "Message" , Value type Json Array

 JSONArray jsonArray=jsonObject.getJsonArray(key);

获取jsonArray后,在循环中使用它,根据Json中的值的类型解析Json。
因此,请检查您是否根据json所持有的值的类型来解析json。

qcuzuvrc

qcuzuvrc7#

Value response of type java.lang.String cannot be converted to JSONArray

您正在尝试将单一对象转换成数组。

lh80um4z

lh80um4z8#

最好使用GSON库而不是手动解析它。GSON你可以在goggling上找到它。并给出了示例。

2ledvvac

2ledvvac9#

例如,如果您有一个如下所示的json代码,则需要了解JSONArray字符串包含在**[]中,而JSONObject字符串包含在{}**中

[
{
userId: 1,
id: 1,
title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
body: "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto"
}
]

在这种情况下,您需要调用JSONArray类并让它获取JSONObject

// Json Array []
JSONArray data = new JSONArray(jsonData);


// JSON Object {}`
  for(int i=0; i<data.length(); i++){
       JSONObject object =  data.getJSONObject(i);
    }

相关问题