這是JSON字符串我要解析解析。安卓:通過JSON
[{"Vehicle":{"display_name":"Anoop","id":"9","tank_capacity":"0.00"},
"Personnel":{"display_name":null,"id":null},
"DeviceLog":{"latitude":"11.182010","longitude":"75.870165","direction":"0","place":"0","speed":"0.00","stop_time":"15245","status":"1","tank_status":"0.00","mileage":"0.00","alerts":null,"kms_left":"0.00","gps_time":"16-07-2011 10:47:23"}}]
我試過下面的代碼。我的目標是從log中獲取從json中檢索的值。
JSONObject json=new JSONObject(result1);
JSONObject vehicle=json.getJSONObject("Vehicle");
JSONObject deviceLog=json.getJSONObject("DeviceLog");
String vehicleName=vehicle.getString("display_name");
int vehicleId=vehicle.getInt("id");
Double latitude=deviceLog.getDouble("latitude");
Double longitude=deviceLog.getDouble("longitude");
System.out.println(vehicleId +" "+ vehicleName+ " "+ latitude+" "+longitude);
我不知道這個代碼。這是我的logcat顯示。
12-03 13:36:32.983: W/System.err(693): org.json.JSONException: A JSONObject text must begin with '{' at character 1 of [{"Vehicle":{"display_name":"galaxy","id":"14","tank_capacity":"0.00"},"Personnel":{"display_name":null,"id":null},"DeviceLog":{"latitude":"9.589788","longitude":"76.530592","direction":"0","place":"Kumarakom Lake Resort(0.12km)","speed":"0.00","stop_time":"704","status":"1","tank_status":"0.00","mileage":"0.00","alerts":null,"kms_left":"0.00","gps_time":"03-12-2011 14:12:12"}}]
12-03 13:36:33.054: W/System.err(693): at org.json.JSONTokener.syntaxError(JSONTokener.java:448)
12-03 13:36:33.054: W/System.err(693): at org.json.JSONObject.<init>(JSONObject.java:178)
12-03 13:36:33.063: W/System.err(693): at org.json.JSONObject.<init>(JSONObject.java:246)
12-03 13:36:33.063: W/System.err(693): at com.rekonsult.signon.Home.createvehicleSubList(Home.java:56)
12-03 13:36:33.063: W/System.err(693): at com.rekonsult.signon.Home.onCreate(Home.java:35)
12-03 13:36:33.063: W/System.err(693): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
12-03 13:36:33.063: W/System.err(693): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
12-03 13:36:33.063: W/System.err(693): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
12-03 13:36:33.073: W/System.err(693): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
12-03 13:36:33.073: W/System.err(693): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
12-03 13:36:33.073: W/System.err(693): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 13:36:33.073: W/System.err(693): at android.os.Looper.loop(Looper.java:123)
12-03 13:36:33.073: W/System.err(693): at android.app.ActivityThread.main(ActivityThread.java:4203)
12-03 13:36:33.073: W/System.err(693): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 13:36:33.073: W/System.err(693): at java.lang.reflect.Method.invoke(Method.java:521)
12-03 13:36:33.084: W/System.err(693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-03 13:36:33.084: W/System.err(693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-03 13:36:33.084: W/System.err(693): at dalvik.system.NativeStart.main(Native Method)
你從哪裏得到這個JSON響應?從任何鏈接或您創建的JSON格式文本。 – Venky
不,我得到這個從aremote服務器作爲帖子.. – freshDroid
嘗試並打印出你在'result1'字符串中得到:看起來像錯誤在那裏。 – gianpi