【问题标题】:Getting All the values of this output json获取此输出 json 的所有值
【发布时间】:2017-01-29 21:57:04
【问题描述】:

我想提取这些咖啡的名称,这是我输出的摘录,因为我有 1000 个名称,我想自动提取它们:

results =     (
            {
        geometry =             {
            location =                 {
                lat = "-33.3979227";
                lng = "-70.58503859999999";
            };
            viewport =                 {
                northeast =                     {
                    lat = "-33.39783990000001";
                    lng = "-70.58502229999999";
                };
                southwest =                     {
                    lat = "-33.39795669999999";
                    lng = "-70.58507830000001";
                };
            };
        };
        id = 46354da06de96a36c5c44a5fa05a10f8f83f8edd;
        name = "Hotel Bidasoa";
        "opening_hours" =             {
            "open_now" = 1;
            "weekday_text" =                 (
            );
        };
            }
        );
        "place_id" = ChIJ4dfUCC7PYpYRRDkSNifrfBE;
        rating = "4.7";
        scope = GOOGLE;
        types =             (
            cafe,
            lodging,
            food,
            store,
            "point_of_interest",
            establishment
        );
        vicinity = "Avenida Vitacura 4873, Santiago, Santiago";
    },
            {
        geometry =             {
            location =                 {
                lat = "-33.37900460000001";
                lng = "-70.55533029999999";
            };
            viewport =                 {
                northeast =                     {
                    lat = "-33.37897230000002";
                    lng = "-70.5553148";
                };
                southwest =                     {
                    lat = "-33.37910149999999";
                    lng = "-70.55537679999999";
                };
            };
        };
        id = c451d2146b7a065fa1afd0ffa39353a4b1cae178;
        name = "Ceibo Emporio Cafeter\U00eda";
        "opening_hours" =             {
            "open_now" = 0;
            "weekday_text" =                 (
            );
        };

这是我的代码,但只打印我想要的名字,因为我有 1000 个名字:

let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                print (jsonResult)

                    if let nombre = ((jsonResult["results"]as?NSArray)?[0]as?NSDictionary)?["name"] {
                        print (nombre)
                    }

【问题讨论】:

    标签: json swift parsing nsarray nsdictionary


    【解决方案1】:

    一如既往:

    • 从不在 Swift 中解析 JSON 时使用 NSDictionary / NSArray
    • 从不在 Swift 中使用 mutableContainers。完全没用。

    要获取results 数组中的所有项目,请使用循环,为了方便和可读性,请使用类型别名:

    typealias JSONDictionary = [String:Any]
    
    if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? JSONDictionary {
    
       print (jsonResult)
       if let results = jsonResult["results"] as? [JSONDictionary] {
          for result in results {
              print(result["name"] as? String ?? "n/a")
              if let geometry = result["geometry"] as? JSONDictionary,
                 let location = geometry["location"] as? JSONDictionary {
                    let lat = location["lat"] as? Double ?? 0.0
                    let lng = location["lng"] as? Double ?? 0.0
                    print("latitude: \(lat)")
                    print("longitude: \(lng)")
              }
          }
       }
    }
    

    【讨论】:

    • 以及我如何访问几何 - 位置 - 纬度?
    • 这段代码显示了 n/a 与 lat 和 lon,我不知道为什么 :(
    • 来自 JSON 输出 - 实际上是字典表示 - latlng 似乎是字符串,但它可能是数字。您应该已经发布了 JSON 字符串。我编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多