Android 获取JSONobject中的JSONobject$1时出错
如何解决此错误,请提出建议 在JSONObject中传递坐标时出错。映射框标记给出空指针异常错误 API的响应Android 获取JSONobject中的JSONobject$1时出错,android,json,Android,Json,如何解决此错误,请提出建议 在JSONObject中传递坐标时出错。映射框标记给出空指针异常错误 API的响应 [ { "SrNumber": "45", "LatLng": { "H": 52.3743585, "L": 4.8904904 }, "Category": ",Apparel & Shoes,", "Detail": {
[
{
"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=newJSONARRAYREQUEST(urlbase,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
字符串img=“”;
for(int i=0;i您在解析区域中犯了错误。我假设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
尽量使用like
optJsonObject(“LatLng”)
,optDouble(“H”)
我想您在JsonArraynull
中的“LatLng”
字段中找到了某个地方,因此它无法转换为JsonObject
因此,尝试将解析数据与示例jsonObject.optJsonObject(“LatLng”)
使用
notnull
condition也请添加代码如何解析JSONArray?@Bunny请立即检查您是否可以打印错误以及您完成的整个类的代码?是的,我在最后的问题中显示了错误。现在检查代码您的异常没有详细说明,因为它没有显示崩溃的行号。请ost详细信息creash报告。此外,您可能会在许多地方获得NullPointerException。另外,使用optString()代替json中的getString()。这将避免在没有字段(如地址、关于、评级等)的情况下获得异常。当您要对“关于”等字段进行操作时,请执行空检查。因此,请让我知道我的代码中需要更改什么。我没有使用改型。
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)
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