Swift 3 - SwiftyJSON字符串返回null

时间:2017-03-12 19:09:57

标签: json swift swifty-json

我使用SwiftyJSON从Google Places API下载JSON(json如下)

{
   "html_attributions" : [],
   "results" : [
      "formatted_address" : "153A Main St, East Calder, Livingston EH53, United Kingdom",
     "geometry" : {
        "location" : {
           "lat" : 55.8959462,
           "lng" : -3.4622283
        },
        "viewport" : {
           "northeast" : {
              "lat" : 55.8973141302915,
              "lng" : -3.460921669708497
           },
           "southwest" : {
              "lat" : 55.8946161697085,
              "lng" : -3.463619630291501
           }
        }
     },
     "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
     "id" : "4e89b3f89f67b043ec79e7dcb5574489bae409f7",
     "name" : "Ink Well Tattoo Studio",
     "opening_hours" : {
        "exceptional_date" : [],
        "open_now" : false,
        "weekday_text" : []
     },
     "photos" : [
        {
           "height" : 443,
           "html_attributions" : [
              "\u003ca href=\"https://maps.google.com/maps/contrib/112667948691916642485/photos\"\u003eRobert Herschell\u003c/a\u003e"
           ],
           "photo_reference" : "CoQBdwAAAElNWvg4vO6E6HvU2srxmeI4gDd-qIS9wXPkm_0CFgJ9ctzQTUeCRKJI-0GTajrBcy9UIJo0DdtRN-T2LgyRRm6G1VwaN7zyQSlbz-tadjMB6SmPQA1hSdTjdcwC3s3vlr72AGK8mwlkBI8-z_UYu7T915TXVESaI2_cRw3rs_woEhCzOzky--6PJ-bAqYM401HIGhSKbGY7dLvJxhqG9-Z7SmQIxBV4uQ",
           "width" : 450
        }
     ],
     "place_id" : "ChIJudHXfnjbh0gRxikhSbbkZzU",
     "rating" : 5,
     "reference" : "CmRRAAAAbgLWU0cl7tbE04cc4VyHf7407xXydh88Ld3R2B1Kq9TIfOC2XHjXIM8BmKkhx_WS2YkA6cVHmSESrx8SGbAw0vpfcFchcEyQOaHEJqc-iY4d3UWvlEWw4DmbaOzyAyBtEhBC4X_b7g-qGcvcRDzEv3WIGhRYMy0MbZUaUqTYWGIy_JWDrfJECg",
     "types" : [ "store", "point_of_interest", "establishment" ]
  }
]

我可以使用SwiftyJSON从JSON中的每个结果中获取地址和名称并填充我的表

let row = (indexPath as NSIndexPath).row
cell?.lblShopTitle.text = self.json["results"][row]["name"].string

然而我试图获取photo_reference字符串,我似乎无法访问它,因为它不断返回为null

let imgRef = self.json["results"][row]["photos"]["photo_reference"]

1 个答案:

答案 0 :(得分:2)

请仔细阅读JSON。

  • []代表一个数组。
  • {}代表字典。

因此,键photos的对象是一个数组。所以最有可能

let imgRef = self.json["results"][row]["photos"][0]["photo_reference"]