从谷歌地图获取JSON数据与swift 3

2020年11月26日 80点热度 0条评论 来源: Yoav Taieb

我当前在ios swift 3应用程序项目中使用谷歌地图数据与JSON,但是当我开始编译时,我无法获得[“results”]的值:从谷歌地图获取JSON数据与swift 3

if let results = dic["results"] as? JSONDictionary { 
           print(results) 
          } 

虽然字典包含此:

["results": <__NSSingleObjectArrayI 0x155d73c0>(
{ 
    "address_components" =  (
       { 
      "long_name" = Paris; 
      "short_name" = Paris; 
      types =    (
       locality, 
       political 
      ); 
     }, 
       { 
      "long_name" = Paris; 
      "short_name" = Paris; 
      types =    (
       "administrative_area_level_2", 
       political 
      ); 
     }, 
       { 
      "long_name" = "\U00cele-de-France"; 
      "short_name" = "\U00cele-de-France"; 
      types =    (
       "administrative_area_level_1", 
       political 
      ); 
     }, 
       { 
      "long_name" = France; 
      "short_name" = FR; 
      types =    (
       country, 
       political 
      ); 
     } 
    ); 
    "formatted_address" = "Paris, France"; 
    geometry =  { 
     bounds =   { 
      northeast =    { 
       lat = "48.9021449"; 
       lng = "2.4699209"; 
      }; 
      southwest =    { 
       lat = "48.815573"; 
       lng = "2.225193"; 
      }; 
     }; 
     location =   { 
      lat = "48.856614"; 
      lng = "2.3522219"; 
     }; 
     "location_type" = APPROXIMATE; 
     viewport =   { 
      northeast =    { 
       lat = "48.9021449"; 
       lng = "2.4699209"; 
      }; 
      southwest =    { 
       lat = "48.815573"; 
       lng = "2.225193"; 
      }; 
     }; 
    }; 
    "place_id" = ChIJD7fiBh9u5kcRYJSMaMOCCwQ; 
    types =  (
     locality, 
     political 
    ); 
} 
) 
, "status": OK] 

虽然JSONDictionary = [字符串:任何]

有什么问题吗?谢谢。


===========解决方案如下:

铸外JSON作为[字符串:任何],因此我们将做了以下的方法:

假定已经铸成[String: Any]的DIC然后通过铸造结果值的内容继续进行,这是一个阵列。

if let results = dic["results"] as? Array<Any> { 
    print(results) 
} 

然后,您应该能够进一步打印和抽象JSON。

    原文作者:Yoav Taieb
    原文地址: https://stackoverflow.com/q/40061443
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。