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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20653106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:21:37  来源:igfitidea点击:

JSON type mismatch or org.json.jsonecxeption

javaandroidjsonandroid-asynctask

提问by Vivek Warde

The link is http://iipacademy.in/askpoll/ten_feed.php

链接是 http://iipacademy.in/askpoll/ten_feed.php

exception is in onPostExecute() method (4th line) :

异常在 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();
}

LOGCAT:

逻辑猫:

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.access0(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

Message is an array so what should be its code or how to can it be solved ?

消息是一个数组,那么它的代码应该是什么或者如何解决?

Thanks in advance . . .

提前致谢 。. .

采纳答案by Raghunandan

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

Looks like response is a string not a json array

看起来响应是一个字符串而不是一个 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",

The result is a json object not json array

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

JSONArray jsonArray = new JSONArray(result);

Should be

应该

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

回答by stefana

Result is not an array, Message is.

结果不是数组,消息是。

回答by Mario Stoilov

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

You are trying to convert a single object into an array.

您正在尝试将单个对象转换为数组。

回答by Damien R.

Try this:

尝试这个:

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

As mario and nfear said, you are trying to cast a JSONObject into a JSONArray.

正如 mario 和 nfear 所说,您正在尝试将 JSONObject 转换为 JSONArray。

回答by ρяσ?ρ?я K

String content JSONObject as root element instead of JSONArray.to get MessageJSONArray from String you should first get responseJSONObject then get MessageJSONArray as:

字符串内容 JSONObject 作为根元素而不是MessageJSONArray。要从 String获取JSONArray,您应该首先获取responseJSONObject 然后获取MessageJSONArray 为:

 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.....

回答by SathishKumar

Try this,

尝试这个,

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

    }
} 

回答by Dinesh Prajapati

better to use GSON library instead of parsing it manually. GSON you can find it on goggling. and sample examples are given with that.

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

回答by pyus13

The JSON you have posted is having

您发布的 JSON 具有

// Key "response" , Value type JsonObject

 JSONObject jsonObject=jsonObjectOfRsponse.getJsonObject(key);

//Key "Message" , Value type Json Array

 JSONArray jsonArray=jsonObject.getJsonArray(key);

After getting the jsonArray, use this in a loop to parse the Json according to the type of value inside it.

得到jsonArray后,循环使用this,根据里面的值类型解析Json。

So check if you are parsing the json according to the type of value it is holding.

因此,请检查您是否根据其持有的值类型解析 json。