Java:谷歌反向地理编码与杰克逊API



我想使用谷歌地图API获取地址。在我的项目中,我们使用杰克逊解析器。

我想知道,我怎样才能得到我预期的结果,即:-

"A10, Dhamidhar Rd, Yashkamal Society, Vasna, Ahmedabad, Gujarat 380007, India"

我想忽略 API 中的所有其他字段。因为 json 文件中的对象太多。 只想获取:

" "formatted_address" : "A10, Dhamidhar Rd, Yashkamal Society, Vasna, Ahmedabad, Gujarat 380007, India","

http://maps.google.com/maps/api/geocode/json?latlng=23.0043673,72.5411868999996&sensor=false

谢谢

这是我尝试过的

package com.example.api.batch;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.JSONPObject;
public class GeocodeAddressParser {
@SuppressWarnings("deprecation")
public void getLocationInfo( String lat, String lng) throws JsonProcessingException, IOException {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false");
@SuppressWarnings("resource")
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
ObjectMapper mapper = new ObjectMapper();
JsonNode array =  mapper.readValue(stringBuilder.toString(), JsonNode.class);
JsonNode object = array.get(0);
String reportKey = object.get("results").textValue();
//      logger.info("ExportController : generatesExportExcel : parameters: {}", reportKey);
//      System.out.println("Map Keys:n"+rawData.getStatus());
//      
//      List<GeocodeGetResult> locations = rawData.getResults();
//      
//      
//      for(int i=0; i < locations.size(); i++){
//          GeocodeGetResult object = locations.get(i);
//          System.out.println(object);
//      }

//      for(int i=0; i < locations.size(); i++){
//          SourceLocation object = locations.get(i);
//          //System.out.println(object.getClass().getName()+" "+object);
//          SourceLocation converted = convertSourceLocation(object);
//          System.out.println(converted);
//          toBeInserted.add(converted);
//      }
//  JSONPObject jsonObject = new JSONPObject();
//      try {
//      //  ObjectNode node = mapper.createObjectNode();
//          JsonNode actualObj = mapper.readTree(stringBuilder.toString());
//         // jsonObject = new JSONObject();
//      } catch (JsonParseException e) {
//          e.printStackTrace();
//      }
//      return mapper;
}

public static void main(String[] args) throws JsonProcessingException, IOException{
GeocodeAddressParser ref = new GeocodeAddressParser();
ref.getLocationInfo("23.0043673","72.5411868999996"); 
//      ObjectMapper location;
//      String location_string;
//      try {
//          //Get JSON Array called "results" and then get the 0th complete object as JSON        
//          location = ret.getJSONArray("results").getJSONObject(0); 
//          // Get the value of the attribute whose name is "formatted_string"
//          location_string = location.getString("formatted_address");
//          Log.d("test", "formattted address:" + location_string);
//      } catch (JSONException e1) {
//          e1.printStackTrace();
//
//      }
}
}

更改getLocationInfo方法并替换以下部分。

ObjectMapper mapper = new ObjectMapper();
JsonNode array =  mapper.readValue(stringBuilder.toString(), JsonNode.class);
JsonNode object = array.get("results").get(0);
String reportKey = object.get("formatted_address").textValue();
System.out.println(reportKey);

在您给定的代码中,

JsonNode object = array.get(0);

这将返回null(object包含null(,因为来自 api 链接的结果将返回一个对象而不是数组。因此,这里没有0元素。

相关内容

  • 没有找到相关文章

最新更新