尝试从字符串创建JSONObject时遇到一些问题。
try {
JSONObject jsonObjMain = new JSONObject(myjsonstring);
final JSONArray jsonArray = jsonObjMain.getJSONArray("Waypoints");
AsyncTask.execute(() -> {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj;
try {
jsonObj = jsonArray.getJSONObject(i);
latL.add(jsonObj.getDouble("Latitude"));
lonL.add(jsonObj.getDouble("Longitude"));
name.add(jsonObj.getString("Name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
错误日志:
2018-12-28 14:06:32.394 30282-30408/com.virtualflight.virtuallink I/System.out: Message received from the server : �����{"Result":0,"Type":"Fds.IFAPI.APIFlightPlan","Bearing":97.74833,"DesiredTrack":113.921631,"DistanceToDestination":20.7412319,"DistanceToNext":20.7412319,"ETAToDestination":NaN,"ETAToNext":NaN,"ETEToDestination":3.63384724,"ETEToNext":3.63384724,"ICAO":null,"NextWaypointLatitude":37.69588,"NextWaypointLongitude":-122.455284,"Track":284.077728,"WaypointName":"DUXBY","Waypoints":[{"Code":"WPT","Latitude":37.613646941754411,"Longitude":-122.35739898939444,"Name":"WPT"},{"Code":null,"Latitude":37.6826,"Longitude":-122.52375,"Name":"KYNNG"},{"Code":null,"Latitude":37.72509444,"Longitude":-122.5029,"Name":"VPSLM"},{"Code":null,"Latitude":37.69588056,"Longitude":-122.45528611,"Name":"DUXBY"}]}
2018-12-28 14:06:32.396 30282-30408 / com.virtualflight.virtuallink W / System.err:org.json.JSONException:禁止的数值:NaN 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSON.checkDouble(JSON.java:25) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSONObject.put(JSONObject.java:265) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSONTokener.readObject(JSONTokener.java:384) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSONTokener.nextValue(JSONTokener.java:100) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSONObject。(JSONObject.java:159) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于org.json.JSONObject。(JSONObject.java:176) 2018-12-28 14:06:32.397 30282-30408 / com.virtualflight.virtuallink W / System.err:位于com.virtualflight.virtuallink.MainActivity $ SendCommand.doInBackground(MainActivity.java:174) 2018-12-28 14:06:32.398 30282-30408 / com.virtualflight.virtuallink W / System.err:位于com.virtualflight.virtuallink.MainActivity $ SendCommand.doInBackground(MainActivity.java:116) 2018-12-28 14:06:32.398 30282-30408 / com.virtualflight.virtuallink W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:333) 2018-12-28 14:06:32.398 30282-30408 / com.virtualflight.virtuallink W / System.err:位于java.util.concurrent.FutureTask.run(FutureTask.java:266) 2018-12-28 14:06:32.398 30282-30408 / com.virtualflight.virtuallink W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245) 2018-12-28 14:06:32.398 30282-30408 / com.virtualflight.virtuallink W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2018-12-28 14:06:32.399 30282-30408 / com.virtualflight.virtuallink W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641) 2018-12-28 14:06:32.399 30282-30408 / com.virtualflight.virtuallink W / System.err:位于java.lang.Thread.run(Thread.java:764)
现在,我想我知道问题出在哪里,但我不知道如何实际解决。本质上,我认为这是因为“ ETAToDestination”和“ ETAToNext”返回“ NaN”值而不是Integer或Double,因此无法将字符串转换为JSONObject。在某些情况下,这两个值中将包含数字值而不是NaN。
也许有某种方法可以抑制该特定代码行上的错误?
在此先感谢您,对我的问题内容的草率格式表示抱歉。
答案 0 :(得分:0)
因此,最简单的解决方案之一是使用所需的字符串替换NaN。
myjsonstring = myjsonstring.replaceAll("NaN", -1 /*Or whatever you need*/);
//Now you can do your work as it is
JSONObject jsonObjMain = new JSONObject(myjsonstring);
答案 1 :(得分:0)
double defaultValue = 0.0;
double myDouble = jsonObj.getDouble("Latitude");
latL.add(Double.isNaN(myDouble) ? defaultValue : myDouble);