在JSONObject

时间:2019-08-27 06:54:55

标签: android json

如何解决此错误,请提出建议。

在JSONObject中传递坐标时出错。和mapbox标记会给出空指针异常错误。

API的响应

[
    {
        "SrNumber": "45",
        "LatLng": {
            "H": 52.3743585,
            "L": 4.8904904
        },
        "Category": ",Apparel & Shoes,",
        "Detail": {
            "name": "United Nude | Amsterdam"
        },
        "location": "Amsterdam",
        "place_title": "United Nude | Amsterdam"
    }
]

JsonArrayRequest jsonArrayRequest =新的JsonArrayRequest(urlbase,新的Response.Listener(){                 @Override                 公共无效onResponse(JSONArray response){                     字符串img =“ https://s3.amazonaws.com/retail-safari-new-app/”;                     for(int i = 0; i

                    try {
                        JSONObject jsonObject = response.getJSONObject ( i );
                        String category = jsonObject.getString ( "Category" );
                        category = category.replace ( ",", "" );
                        String DetailsString = jsonObject.getString ( "Detail" );
                        JSONObject DetailsObj = new JSONObject ( DetailsString );
                        Title = DetailsObj.getString ( "name" );

                        JSONObject coordinateJsonObj = jsonObject.getJSONObject("LatLng");
                      //  JSONObject coordinatesObj = new JSONObject ( coordinates );
                        lat = coordinateJsonObj.getDouble ( "H" );
                        lng = coordinateJsonObj.getDouble ( "L" );

                        LatLng latLng = new LatLng (coordinateJsonObj.getDouble ( "H" ),coordinateJsonObj.getDouble ( "L" ) );

                        IconFactory iconFactory = (IconFactory) IconFactory.getInstance ( MapActivity.this );
                        Icon icon = iconFactory.fromResource ( R.drawable.marker );

                        MarkerOptions markerOptions = (MarkerOptions) new MarkerOptions ()
                                .position ( latLng )
                                .title ( Title )
                                .snippet ( Address )
                                .icon ( icon );
                        mapboxMap.addMarker ( markerOptions );
                        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom ( latLng,  12);
                        mapboxMap.moveCamera ( cameraUpdate );
                        mapboxMap.animateCamera(cameraUpdate);

                        Location locationA = new Location ( "point A" );

                        locationA.setLatitude ( locationlat );
                        locationA.setLongitude ( locationlng );


                        Location locationB = new Location ( "point B" );

                        locationB.setLatitude ( lat );
                        locationB.setLongitude ( lng );

                        float distance = locationA.distanceTo ( locationB );   // this gives distance in meters
                        distance = distance / 1000;
                        DecimalFormat decimalFormat = new DecimalFormat ( "#.##" );
                        distance = Float.valueOf ( decimalFormat.format ( distance * 0.621 ) );
                        String DistanceInMiles = String.valueOf ( distance );


                        DistanceInMiles = DistanceInMiles + " mi away";

                        MapActivityModel places = new MapActivityModel ();
                        places.setCategory ( category );
                        places.setTitle ( Title );
                         places.setLat ( lat );
                        places.setLng ( lng );

                        PlacesList.add ( places );

                        Collections.sort ( PlacesList, new Comparator<MapActivityModel>() {
                            @Override
                            public int compare(MapActivityModel t1, MapActivityModel t2) {
                                return new String ( t1.getDistance () ).compareTo ( t2.getDistance () );
                            }
                        } );


                    } catch (JSONException e) {
                        e.printStackTrace ();
                     //   progressDialog.dismiss ();
                    }
                }
                adapter.notifyDataSetChanged ();
          //      progressDialog.dismiss ();
            }
        }, new Response.ErrorListener () {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e ( "Volley", error.toString () );
           //     progressDialog.dismiss ();

            }
        } );
        RequestQueue requestQueue = Volley.newRequestQueue ( this );
        requestQueue.add ( jsonArrayRequest );
error getting: W/System.err: org.json.JSONException: Value null at LatLng of type org.json.JSONObject$1 cannot be converted to JSONObject
        at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONObject.getJSONObject(JSONObject.java:616)
        at earthnew.retailtrendsinc.www.myapplication.MapActivity$5.onResponse(MapActivity.java:453)
        at earthnew.retailtrendsinc.www.myapplication.MapActivity$5.onResponse(MapActivity.java:429)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:83)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2 个答案:

答案 0 :(得分:1)

尝试像 optJsonObject("LatLng")optDouble("H") 我认为您在null字段的JsonArray "LatLng"中找到了某个地方,因此它无法转换为JsonObject。

因此,请尝试将解析数据与示例jsonObject.optJsonObject("LatLng")一起使用 也使用not null条件

答案 1 :(得分:0)

您在解析区域做错了。我假设jsonObject是将从中获取lat lng的商品的对象。

JSONObject coordinateJsonObj = jsonObject.getJsonObject("LatLng"); //Since LatLng is a json object.

lat = coordinateJsonObj.getDouble ( "H" );
lng = coordinateJsonObj.getDouble ( "L" );

LatLng latLng = new LatLng (lat, lng);

....Rest section