当前位置:  开发笔记 > 编程语言 > 正文

在swift 3.0中解析字典

如何解决《在swift3.0中解析字典》经验,为你挑选了1个好方法。

我使用google places api来获取经度和纬度,但无法解析字典.我正在使用以下代码

 let lat = ((((dic["results"] as! AnyObject).value(forKey: "geometry") as AnyObject).value(forKey: "location") as AnyObject).value(forKey: "lat") as AnyObject).object(0) as! Double

错误:无法调用非功能类型的值'任何?!'

我还检查过nsdictionary而不是Anyobject但是没有用.任何人都可以知道如何正确地从上面的代码中获取lat.

回应:

{
results =     (
            {
        "address_components" =             (
                            {
                "long_name" = Palermo;
                "short_name" = Palermo;
                types =                     (
                    locality,
                    political
                );
            },
                            {
                "long_name" = "Province of Palermo";
                "short_name" = PA;
                types =                     (
                    "administrative_area_level_2",
                    political
                );
            },
                            {
                "long_name" = Sicily;
                "short_name" = Sicily;
                types =                     (
                    "administrative_area_level_1",
                    political
                );
            },
                            {
                "long_name" = Italy;
                "short_name" = IT;
                types =                     (
                    country,
                    political
                );
            }
        );
        "formatted_address" = "Palermo, Italy";
        geometry =             {
            bounds =                 {
                northeast =                     {
                    lat = "38.219548";
                    lng = "13.4471566";
                };
                southwest =                     {
                    lat = "38.0615392";
                    lng = "13.2674205";
                };
            };
            location =                 {
                lat = "38.1156879";
                lng = "13.3612671";
            };
            "location_type" = APPROXIMATE;
            viewport =                 {
                northeast =                     {
                    lat = "38.2194316";
                    lng = "13.4471566";
                };
                southwest =                     {
                    lat = "38.0615392";
                    lng = "13.2674205";
                };
            };
        };
        "place_id" = ChIJmdBOgcnoGRMRgNg7IywECwo;
        types =             (
            locality,
            political
        );
    }
);
status = OK;

}



1> vadian..:

您必须分多步解析JSON,为清楚起见,我建议使用类型别名:

typealias JSONDictionary = [String:Any]

    let results = dic["results"] as? [JSONDictionary] {
      for result in results {
        if let geometry = result["geometry"] as? JSONDictionary,
           let location = geometry["location"] as? JSONDictionary {
              if let lat = location["lat"] as? Double,
                 let lng = location["lng"] as? Double {
                   print(lat, lng)
              }
        }
     }
   }

Swift中的一些规则:

除非你绝对没有选择,否则不要使用NSDictionary/ NSArray.

Swift 3中的JSON字典是[String:Any]一个JSON数组[[String:Any]].

铸造Any类型下尽可能具体.

从远程服务器解析JSON时,始终使用可选绑定来避免运行时错误.

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有