1

我打电话给这个网址

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

我想响应是空的,我想知道为什么,这里做错了什么?刚刚从响应 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;
4

2 回答 2

2

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

来自下面悉达多的评论。

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"]
于 2013-06-22T15:24:35.590 回答
1

你从...开始

JsonObject response = new JsonObject().getAsJsonObject(jsonResults.toString());

这是没有意义的。您创建了一个新的空元素,JsonObject然后尝试使用整个 JSON 字符串作为属性名称从中检索元素。因此,是的……responsenull

您的 JSON 是一个对象。它包含两个字段;results哪个是对象数组,status哪个是String.

通常在使用 Gson 时,您会创建匹配的 Java 类并使用Gson实例来反序列化它们。如果您不想这样做,那么您可以让 GsonJsonParser解析 Json,然后通过各种 Json* 类访问它:

/// jsonResuts is a String containing the JSON you list.
JsonObject obj = new JsonParser().parse(jsonResults).getAsJsonObject();
JsonArray resultsArray = obj.getAsJsonArray("results");

现在您有了对象数组,并且可以使用我在上面使用的 Gson 中的相同方法/类来更深入地研究它。

for (JsonElement e : resultsArray) {
    JsonObject resultsObj = e.getAsJsonObject();
    JsonElement formattedAddress = resultsObject.get("formatted_address");
    String addressAsString = formattedAddress.getAsString();
    System.out.println(addressAsString);
    // The above can be chained and reduced to one line:
    // System.out.println(e.getAsJsonObject().get("formatted_address").getAsString());
}  
于 2013-06-23T03:39:16.587 回答