gpt4 book ai didi

java - Json 解析给出 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 05:41:47 25 4
gpt4 key购买 nike

我正在调用这个 url

http://maps.googleapis.com/maps/api/geocode/json?latlng=18.550455,73.920148&sensor=true

并获得回复(我从中间删除了很多回复,因为它与问题无关。

{
"results" : [
{
"address_components" : [
{
"long_name" : "Towards Wadgao Sheri",
"short_name" : "Towards Wadgao Sheri",
"types" : [ "route" ]
},
{
"long_name" : "411014",
"short_name" : "411014",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Towards Wadgao Sheri, Sainikwadi, Wadgaon Sheri, Pune, Maharashtra 411014, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.55131630,
"lng" : 73.91882219999999
},
"southwest" : {
"lat" : 18.55024130,
"lng" : 73.91866569999999
}
},
"location" : {
"lat" : 18.55077720,
"lng" : 73.91878240
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.55212778029150,
"lng" : 73.92009293029150
},
"southwest" : {
"lat" : 18.54942981970850,
"lng" : 73.91739496970848
}
}
},
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Sainikwadi",
"short_name" : "Sainikwadi",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "411014",
"short_name" : "411014",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Sainikwadi, Wadgaon Sheri, Pune, Maharashtra 411014, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.5551140,
"lng" : 73.92143690
},
"southwest" : {
"lat" : 18.5465270,
"lng" : 73.91406809999999
}
},
"location" : {
"lat" : 18.55092520,
"lng" : 73.91687659999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.5551140,
"lng" : 73.92143690
},
"southwest" : {
"lat" : 18.5465270,
"lng" : 73.91406809999999
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.50447520,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16238590
}
},
"location" : {
"lat" : 20.5936840,
"lng" : 78.962880
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.50447520,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16279560
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}

代码

        JsonObject response = new JsonObject().getAsJsonObject(jsonResults.toString());
>>>NPE JsonArray results = response.getAsJsonArray("results");

来自 json 的响应在 jsonResults 中。我在第二行收到 NPE

我猜 response 是空的,我想知道为什么,这里做错了什么?刚刚从响应 json 中创建了一个 json 对象。我知道我在这里遗漏了一些非常愚蠢的东西。

编辑:

Exception in thread "Thread-0" java.lang.NullPointerException
at com.mcruiseon.ivr.God.getGeoAddress(God.java:141)

编辑 2:

我稍微编辑了 json 响应。请注意。我也在尝试提取 formatted_address

编辑 3 :

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

最佳答案

答案的关键:是否使用Gson,解析json字符串是答案的关键。

来自下面 Siddharth 的评论。

JsonParser parser = new JsonParser();
JsonObject json = parser.parse(jsonResults.toString()).getAsJsonObject() ;
JsonArray results = json.getAsJsonArray("results");
JsonObject result = results.get(0).getAsJsonObject();
customerAddress = result.get("formatted_address").getAsString();

备选方案:

我没有阅读问题的编辑 3 部分,也没有查看 gson 标签。所以我最终提供了以下替代方案。

JSONObject jsono = new JSONObject (myjsonstring);
JSONArray myListsAll= new JSONArray(jsono.getString("results"));
for(int i=0;i<myListsAll.length();i++){
JSONObject jsonObject1 = (JSONObject) myListsAll.get(i);
JSONArray myarray= new JSONArray(jsonObject1.getString("address_components"));
for(int i1=0;i1<myarray.length();i1++)
{
JSONObject jsonobject2 = myarray.getJSONObject(i1);
System.out.println(""+jsonobject2.getString("long_name"));
System.out.println(""+jsonobject2.getString("short_name"));
System.out.println(""+jsonobject2.getString("types"));
}
}

输出

Towards Wadgao Sheri
Towards Wadgao Sheri
["route"]
Sainikwadi
Sainikwadi
["neighborhood","political"]
Wadgaon Sheri
Wadgaon Sheri
["sublocality","political"]
Pune
Pune
["locality","political"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
411014
411014
["postal_code"]
Sainikwadi
Sainikwadi
["neighborhood","political"]
Wadgaon Sheri
Wadgaon Sheri
["sublocality","political"]
Pune
Pune
["locality","political"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
411014
411014
["postal_code"]
Wadgaon Sheri
Wadgaon Sheri
["sublocality","political"]
Pune
Pune
["locality","political"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
411014
411014
["postal_code"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
Pune
Pune
["locality","political"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
Pune
Pune
["administrative_area_level_2","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
Maharashtra
MH
["administrative_area_level_1","political"]
India
IN
["country","political"]
India
IN
["country","political"]

关于java - Json 解析给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251869/

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