在任何人的情况下更新的代码都需要这个的固定.
我的错误:
01-14 22:18:08.165: E/JsON Response:(419): > { "event":[ 01-14 22:18:08.165: E/JsON Response:(419): { 01-14 22:18:08.165: E/JsON Response:(419): "event_name":"Test Event",01-14 22:18:08.165: E/JsON Response:(419): "event_time":"7:00pm",01-14 22:18:08.165: E/JsON Response:(419): "event_price":".00" 01-14 22:18:08.165: E/JsON Response:(419): } 01-14 22:18:08.165: E/JsON Response:(419): ] 01-14 22:18:08.165: E/JsON Response:(419): } 01-14 22:18:08.175: E/Json Error(419): Error: org.Json.JsONException: Value [{"event_price":".00","event_time":"7:00pm","event_name":"Test Event"}] at event of type org.Json.JsONArray cannot be converted to JsONObject
MainActivity.java
package com.example.dba;import org.Json.JsONException;import org.Json.JsONObject;import androID.app.Activity;import androID.content.Intent;import androID.os.AsyncTask;import androID.os.Bundle;import androID.util.Log;public class MainActivity extends Activity {String event_name,event_time,event_price;static JsONObject object =null;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); new PrefetchData().execute();}/** * Async Task to make http call */private class PrefetchData extends AsyncTask<VoID,VoID,VoID> { @OverrIDe protected voID onPreExecute() { super.onPreExecute(); } @OverrIDe protected VoID doInBackground(VoID... arg0) { JsonParser JsonParser = new JsonParser(); String Json = JsonParser.getJsONFromUrl("http://www.website/test.Json"); Log.e("JsON Response: ","> " + Json); if (Json != null) { try { JsONObject parent = new JsONObject(Json); JsONArray eventDetails = parent.getJsONArray("event"); for(int i=0; i < eventDetails.length(); i++) { object = eventDetails.getJsONObject(i); event_name = object.getString("event_name"); event_time = object.getString("event_time"); event_price = object.getString("event_price"); Log.e("JsON","> " + event_name + event_time + event_price ); } } catch (JsONException e) { Log.e("Json Error","Error: " + e.toString()); e.printstacktrace(); } } return null; } @OverrIDe protected voID onPostExecute(VoID result) { super.onPostExecute(result); Intent i = new Intent(MainActivity.this,Events.class); i.putExtra("event_name",event_name); i.putExtra("event_time",event_time); i.putExtra("event_price",event_price); startActivity(i); // close this activity finish(); }}}}解决方法 要更改的相关代码 – 在MainActivity.java的doInBackground()中:
JsONObject eventDetails = parent.getJsONObject("event");
至:
JsONArray eventDetails = parent.getJsONArray("event");总结
以上是内存溢出为你收集整理的android – org.json.jsonarray无法转换为jsonobject错误全部内容,希望文章能够帮你解决android – org.json.jsonarray无法转换为jsonobject错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)