JSON type mismatch or org.json.jsonecxeption(JSON 类型不匹配或 org.json.jsonecxeption)
问题描述
链接是http://iipacademy.in/askpoll/ten_feed.php
异常在 onPostExecute() 方法中(第 4 行):
exception is in onPostExecute() method (4th line) :
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
Message 是一个数组,那么它的代码应该是什么或者如何解决?
Message is an array so what should be its code or how to can it be solved ?
谢谢在进步 ...
推荐答案
org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray
看起来响应是一个字符串而不是一个 json 数组
Looks like response is a string not a json array
{ // 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数组
The result is a json object not json array
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);
}
这篇关于JSON 类型不匹配或 org.json.jsonecxeption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON 类型不匹配或 org.json.jsonecxeption
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01