1

私はこの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 にあります。2行目でNPEを取得しています

私は応答がnullだと思います、なぜだろう、ここで何が間違っているのですか? 応答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

1

あなたはから始めます...

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

これは意味がありません。新しい空の を作成し、JsonObjectJSON 文字列全体をプロパティ名として使用して、そこから要素を取得しようとしました。したがって、はい...responseですnull

JSON はオブジェクトです。2 つのフィールドが含まれています。resultsこれはオブジェクトの配列で、statusこれはStringです。

通常、Gson を使用する場合は、一致する Java クラスを作成し、Gsonインスタンスを使用してそれらに逆シリアル化します。それをしたくない場合はJsonParser、Gson で 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 に答える