- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用了这个有用的code snippet为了在我的 android 应用程序中获取当前位置,现在我想获取 route
(街道地址),street_number
, postal_code
和 country
(始终 long_name
)在 Google 地理编码 JSON 对象中。目前我使用 do - while 循环然后拆分 formatted_address
以获得所需的结果,但我认为通过其键访问该值会更好。
JSON 看起来像这样:
{
"results": [
{
"types": [
"street_address"
],
"formatted_address": "RotenturmstraÃe 2, 1010 Vienna, Austria",
"address_components": [
{
"types": [
"street_number"
],
"short_name": "2",
"long_name": "2"
},
{
"types": [
"route"
],
"short_name": "Rotenturmstr.",
"long_name": "RotenturmstraÃe"
},
{
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"short_name": "Innere Stadt",
"long_name": "Innere Stadt"
},
{
"types": [
"locality",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"country",
"political"
],
"short_name": "AT",
"long_name": "Austria"
},
{
"types": [
"postal_code"
],
"short_name": "1010",
"long_name": "1010"
}
],
"geometry": {
"viewport": {
"southwest": {
"lng": 16.3719497197085,
"lat": 48.20797491970851
},
"northeast": {
"lng": 16.3746476802915,
"lat": 48.2106728802915
}
},
"location": {
"lng": 16.3732987,
"lat": 48.2093239
},
"location_type": "ROOFTOP"
}
},
{
"types": [
"neighborhood",
"political"
],
"formatted_address": "Stephansdom, 1010 Vienna, Austria",
"address_components": [
{
"types": [
"neighborhood",
"political"
],
"short_name": "Stephansdom",
"long_name": "Stephansdom"
},
{
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"short_name": "Innere Stadt",
"long_name": "Innere Stadt"
},
{
"types": [
"locality",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"country",
"political"
],
"short_name": "AT",
"long_name": "Austria"
},
{
"types": [
"postal_code"
],
"short_name": "1010",
"long_name": "1010"
}
],
"geometry": {
"bounds": {
"southwest": {
"lng": 16.3663459,
"lat": 48.2016949
},
"northeast": {
"lng": 16.382804,
"lat": 48.21546559999999
}
},
"viewport": {
"southwest": {
"lng": 16.3663459,
"lat": 48.2016949
},
"northeast": {
"lng": 16.382804,
"lat": 48.21546559999999
}
},
"location": {
"lng": 16.3767451,
"lat": 48.2083206
},
"location_type": "APPROXIMATE"
}
},
{
"types": [
"postal_code"
],
"formatted_address": "1010 Vienna, Austria",
"address_components": [
{
"types": [
"postal_code"
],
"short_name": "1010",
"long_name": "1010"
},
{
"types": [
"locality",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"country",
"political"
],
"short_name": "AT",
"long_name": "Austria"
}
],
"geometry": {
"bounds": {
"southwest": {
"lng": 16.3552218,
"lat": 48.1992606
},
"northeast": {
"lng": 16.3853131,
"lat": 48.2185876
}
},
"viewport": {
"southwest": {
"lng": 16.3552218,
"lat": 48.1992606
},
"northeast": {
"lng": 16.3853131,
"lat": 48.2185876
}
},
"location": {
"lng": 16.3677601,
"lat": 48.2083556
},
"location_type": "APPROXIMATE"
}
},
{
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"formatted_address": "Innere Stadt, 1010 Vienna, Austria",
"address_components": [
{
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"short_name": "Innere Stadt",
"long_name": "Innere Stadt"
},
{
"types": [
"locality",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"country",
"political"
],
"short_name": "AT",
"long_name": "Austria"
},
{
"types": [
"postal_code"
],
"short_name": "1010",
"long_name": "1010"
}
],
"geometry": {
"bounds": {
"southwest": {
"lng": 16.3552218,
"lat": 48.1992606
},
"northeast": {
"lng": 16.3853131,
"lat": 48.2185876
}
},
"viewport": {
"southwest": {
"lng": 16.3552218,
"lat": 48.1992606
},
"northeast": {
"lng": 16.3853131,
"lat": 48.2185876
}
},
"location": {
"lng": 16.3697428,
"lat": 48.2088774
},
"location_type": "APPROXIMATE"
}
},
{
"types": [
"administrative_area_level_1",
"political"
],
"formatted_address": "Vienna, Austria",
"address_components": [
{
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "Vienna",
"long_name": "Vienna"
},
{
"types": [
"country",
"political"
],
"short_name": "AT",
"long_name": "Austria"
}
],
"geometry": {
"bounds": {
"southwest": {
"lng": 16.1826199,
"lat": 48.1182699
},
"northeast": {
"lng": 16.5774999,
"lat": 48.3230999
}
},
"viewport": {
"southwest": {
"lng": 16.1826199,
"lat": 48.1182699
},
"northeast": {
"lng": 16.5774999,
"lat": 48.3230999
}
},
"location": {
"lng": 16.3738138,
"lat": 48.20823499999999
},
"location_type": "APPROXIMATE"
}
}
]
这是我的代码:
public static JSONObject getLocationInfo(double lat, double lng) {
HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat+","+lng +"&sensor=true");
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) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
public static String getCurrentLocationViaJSON(double lat, double lng) {
JSONObject jsonObj = getLocationInfo(lat, lng);
Log.i("JSON string =>", jsonObj.toString());
String currentLocation = "testing";
String street_address = null;
String postal_code = null;
try {
String status = jsonObj.getString("status").toString();
Log.i("status", status);
if(status.equalsIgnoreCase("OK")){
JSONArray results = jsonObj.getJSONArray("results");
int i = 0;
Log.i("i", i+ "," + results.length() ); //TODO delete this
do{
JSONObject r = results.getJSONObject(i);
JSONArray typesArray = r.getJSONArray("types");
String types = typesArray.getString(0);
if(types.equalsIgnoreCase("street_address")){
street_address = r.getString("formatted_address").split(",")[0];
Log.i("street_address", street_address);
}else if(types.equalsIgnoreCase("postal_code")){
postal_code = r.getString("formatted_address");
Log.i("postal_code", postal_code);
}
if(street_address!=null && postal_code!=null){
currentLocation = street_address + "," + postal_code;
Log.i("Current Location =>", currentLocation); //Delete this
i = results.length();
}
i++;
}while(i<results.length());
Log.i("JSON Geo Locatoin =>", currentLocation);
return currentLocation;
}
} catch (JSONException e) {
Log.e("testing","Failed to load JSON");
e.printStackTrace();
}
return null;
}
如有任何帮助,我们将不胜感激。
最佳答案
在该代码示例中,他们使用 JSONObject,因此您将要使用 getJSONObject(i)
、getJSONArray()
和 getString ()
来解析你的数据。
可能建议使用这样的函数将数据解析为更易读的变量。否则,如果您直接使用它们的 key ,您将得到类似 String street_address = jsonObj.getJSONArray("results").getJSONObject(i).getString("formatted_address")
之类的东西,其中很好,但它很大。
此外,您所说的“最好通过其键访问值”是什么意思有点不清楚。在您的循环中,您正在通过其键访问该值。代码 r.getString("formatted_address")
使用键 formatted_address
访问该键值对的数据。
这是一种可能的解决方案:
public JSONObject getCurrentLocationViaJSON(double lat, double lng) {
// Returns a JSONObject with route (street_address), street_number, postal_code and country
JSONObject jsonObj = getLocationInfo(lat, lng);
Log.i("JSON string =>", jsonObj.toString());
String currentLocation = "testing";
String route = null;
String street_number = null;
String postal_code = null;
String country = null;
try {
String status = jsonObj.getString("status").toString();
Log.i("status", status);
if(status.equalsIgnoreCase("OK")){
JSONArray results = jsonObj.getJSONArray("results");
int i = 0;
Log.i("i", i+ "," + results.length() ); //TODO delete this
do{
JSONObject r = results.getJSONObject(0);
JSONArray addressComponentsArray = r.getJSONArray("address_components");
JSONArray addressComponents = addressComponentsArray.getJSONObject(i);
JSONArray typesArray = addressComponents.getJSONArray(0);
String types = typesArray.getString(0);
if(types.equalsIgnoreCase("street_number")){
street_number = addressComponents.getString("short_name");
Log.i("street_number", street_number);
}else if(types.equalsIgnoreCase("postal_code")){
postal_code = addressComponents.getString("long_name");
Log.i("postal_code", postal_code);
}else if(types.equalsIgnoreCase("country")){
country = addressComponents.getString("long_name");
Log.i("country", country);
}else if(types.equalsIgnoreCase("route")){
route = addressComponents.getString("long_name");
Log.i("postal_code", postal_code);
}
i++;
}while(i<results.length());
Log.i("JSON Geo Locatoin =>", currentLocation);
return currentLocation;
}
} catch (JSONException e) {
Log.e("testing","Failed to load JSON");
e.printStackTrace();
}
JSONObject json = new JSONObject();
json.put("street_number", street_number);
json.put("postal_code", postal_code);
json.put("country ", country);
json.put("route", route);
return json;
}
上述方法将解析出路线(街道地址)、街道编号、邮政编码和国家(总是长名称),将其存储在一个 JSONObject 中,然后返回该对象。您可以使用以下内容访问这些值:
JSONObject addressDetails = getCurrentLocationViaJSON(100.1, 100.1);
String streetNum = addressDetails.getString("street_number");
关于java - 在复杂的 Google 地理编码 JSON 对象中按键获取值 - Java - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214785/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!