gpt4 book ai didi

java - android:通过json解析

转载 作者:行者123 更新时间:2023-12-01 15:39:09 24 4
gpt4 key购买 nike

这是我必须解析的 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"}}]

我尝试了以下代码。我的目标是获取从 logcat 中的 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)

最佳答案

试试这个:

JSONArray rec=new JSONArray(result1);

JSONObject json=rec.getJSONObject(0);
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);

关于java - android:通过json解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com