JSON类型不匹配或org.json.jsonecxeption [英] JSON type mismatch or org.json.jsonecxeption
本文介绍了JSON类型不匹配或org.json.jsonecxeption的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该链接 http://iipacademy.in/askpoll/ten_feed.php
例外是onPostExecute()方法(4号线):
Log.i(结果,结果);
尝试 {
如果(结果!= NULL){
JSONArray jsonArray =新JSONArray(结果); // erreor
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject objJson = jsonArray.getJSONObject(ⅰ);
TopTenGetterSetter OBJ =新TopTenGetterSetter();
obj.setQ(objJson.getString(问题));
obj.setA(objJson.getString(选项1));
obj.setB(objJson.getString(选项2));
obj.setC(objJson.getString(选项3));
obj.setD(objJson.getString(选项4));
polls.add(OBJ);
}
}
}赶上(JSONException E){
e.printStackTrace();
Toast.makeText(getApplicationContext(),错误,
Toast.LENGTH_SHORT).show();
}
logcat的:
三月十二号日至18日:20:45.447:W / System.err的(2790):org.json.JSONException:java.lang.String类型的值响应不能转换为JSONArray
3月12号至18日:20:45.447:W / System.err的(2790):在org.json.JSON.typeMismatch(JSON.java:111)
3月12号至18日:20:45.447:W / System.err的(2790):在org.json.JSONArray< INIT>(JSONArray.java:91)
3月12号至18日:20:45.447:W / System.err的(2790):在org.json.JSONArray< INIT>(JSONArray.java:103)
3月12号至18日:20:45.447:W / System.err的(2790):在com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:188)
3月12号至18日:20:45.447:W / System.err的(2790):在com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:1)
3月12号至18日:20:45.447:W / System.err的(2790):在android.os.AsyncTask.finish(AsyncTask.java:631)
3月12号至18日:20:45.447:W / System.err的(2790):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
3月12号至18日:20:45.447:W / System.err的(2790):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
3月12号至18日:20:45.447:W / System.err的(2790):在android.os.Handler.dispatchMessage(Handler.java:99)
3月12号至18日:20:45.447:W / System.err的(2790):在android.os.Looper.loop(Looper.java:137)
3月12号至18日:20:45.447:W / System.err的(2790):在android.app.ActivityThread.main(ActivityThread.java:5103)
3月12号至18日:20:45.447:W / System.err的(2790):在java.lang.reflect.Method.invokeNative(本机方法)
3月12号至18日:20:45.447:W / System.err的(2790):在java.lang.reflect.Method.invoke(Method.java:525)
3月12号至18日:20:45.447:W / System.err的(2790):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
3月12号至18日:20:45.447:W / System.err的(2790):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
3月12号至18日:20:45.447:W / System.err的(2790):在dalvik.system.NativeStart.main(本机方法)
3月12号至18日:20:45.447:D / dalvikvm(2790):GC_FOR_ALLOC释放5131K,55%免费4437K / 9672K,暂停2毫秒,2毫秒总
信息是一个数组,那么应该怎么了code或如何能不能得到解决?
谢谢 在 提前 。 。
解决方案
org.json.JSONException:java.lang.String类型的值响应不能转换为JSONArray
看起来响应是一个字符串不是一个JSON数组
{// JSON对象节点
响应:{// json对象响应
结果:1,
消息:// JSON数组留言
{// JSON对象节点
pollid:98,
类别:娱乐,
问题:做什么,//字符串
选项1:981.mov
结果是一个JSON对象不是JSON数组
JSONArray jsonArray =新JSONArray(结果);
应
的JSONObject jObj =新的JSONObject的(结果);
JSONObject的响应= jObj.getJSONObject(回应);
// JSONObject的JB =新的JSONObject(响应);
JSONArray JR = response.getJSONArray(信息);
的for(int i = 0; I< jr.length();我++)
{
的JSONObject JB1 = jr.getJSONObject(ⅰ);
字符串的问题= jb1.getString(问题);
Log.i(......,问题);
}
The link is http://iipacademy.in/askpoll/ten_feed.php
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();
}
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.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 is an array so what should be its code or how to can it be solved ?
Thanks in advance . . .
解决方案
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 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
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);
}
这篇关于JSON类型不匹配或org.json.jsonecxeption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文