2
votes

I am having some issues when trying to create a JSONObject from a string.

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();
                }

Error Logs:

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: Forbidden numeric value: NaN 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSON.checkDouble(JSON.java:25) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSONObject.put(JSONObject.java:265) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSONTokener.readObject(JSONTokener.java:384) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:100) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSONObject.(JSONObject.java:159) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at org.json.JSONObject.(JSONObject.java:176) 2018-12-28 14:06:32.397 30282-30408/com.virtualflight.virtuallink W/System.err: at com.virtualflight.virtuallink.MainActivity$SendCommand.doInBackground(MainActivity.java:174) 2018-12-28 14:06:32.398 30282-30408/com.virtualflight.virtuallink W/System.err: at 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: at 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: at java.lang.Thread.run(Thread.java:764)

Now, I think I know what the issue is but I don't know how to actually fix it. In essence, I think it is because "ETAToDestination" and "ETAToNext" return "NaN" values instead of an Integer or Double, therefore the string cannot be converted to a JSONObject. There will be cases where those 2 values will have a numeric value in them instead of NaN.

Maybe there is some way of suppressing the error on that specific line of code?

Thank you in advance and sorry about the sloppy formatting of my questions contents.

2
where are you getting your json string from?Farhan Qasim
@FarhanQasim I'm getting it from a game APIdragos popa
@dragospopa, is that up to your control? Why does it return NaNs and can you make it return something else?M. Prokhorov
If you look here in JSON.stringify part, note 4, NaN is not a valid result for JSON.stringify, it must be a null. JSON spec also doesn't permit NaN values (more precisely, the values not representable by a string of decimal numbers).M. Prokhorov
@M.Prokhorov The API is not in my control. Thanks for the help but Kousik Mandal's answer workeddragos popa

2 Answers

0
votes

So, one of the most easy solution is string replace the NaN with whatever you want.

myjsonstring = myjsonstring.replaceAll("NaN", -1 /*Or whatever you need*/);

//Now you can do your work as it is
JSONObject jsonObjMain = new JSONObject(myjsonstring);
0
votes
double defaultValue = 0.0;

double myDouble = jsonObj.getDouble("Latitude");
latL.add(Double.isNaN(myDouble) ? defaultValue : myDouble);