2012-05-03 45 views
1

引用此https://developers.google.com/maps/documentation/places/#PlaceDetails 我期待JSON结果始终保持所有address_components的详细信息。然而,我的一个搜索结果失踪了street_number,postal_code和其他人之一。查看intellisense透露address_components之一返回了一个administrative_area_level_2。缺少Google JSON详细信息

这使我陷入循环,因为我总是期望即使它们可能是空的,也会返回原始值。我也硬编码的预期回报,像这样:

array.result.address_components(0).long_name 

既然不能指望组件返回所有的结果,我需要一个新的方式来适当地访问它们并找出时,我该怎么办缺少一些。

+0

我们在谈论什么样的编程语言? –

+0

VB.net是我正在使用的。我想我可能不得不使用一个选择案例,并检查每个组件的类型。 – Mitchell

+0

我认为,当涉及到你自己产生的数据时,你绝对不应该进行任何推定。在使用变量之前,确保变量存在。 –

回答

0

下面是我用来解决这个问题的案例陈述。它工作,但不知道它是否是最优雅的解决方案。

Dim street As String = "" 
Dim city As String = "" 
Dim state As String = "" 
Dim country As String = "" 
Dim zip As String = "" 
Dim phone As String = "" 
Dim website As String = "" 

' No guareentee to the order of even if the data will be there so we check each type. ' 
For j = 0 To array.result.address_components.Length - 1 
    Select Case array.result.address_components(j).types(0) 
     Case "street_number" 
      street += array.result.address_components(j).long_name() 
     Case "route" 
      street += " " & array.result.address_components(j).long_name() 
     Case "locality" 
      city = array.result.address_components(j).long_name() 
     Case "administrative_area_level_1" 
      state = array.result.address_components(j).long_name() 
     Case "country" 
      country = array.result.address_components(j).long_name() 
     Case "postal_code" 
      zip = array.result.address_components(j).long_name() 
    End Select 
Next 

我基本上循环访问address_components并检查它们的类型值。如果这个值是我正在寻找的值,我将它赋值。之后我会在街道上拨打Trim()以删除我添加的空白区域,如果没有街道号。