Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取此输出json的所有值_Json_Swift_Parsing_Nsarray_Nsdictionary - Fatal编程技术网

获取此输出json的所有值

获取此输出json的所有值,json,swift,parsing,nsarray,nsdictionary,Json,Swift,Parsing,Nsarray,Nsdictionary,我想提取这些Coffe的名称这是我输出的一个摘录,因为我有1000个名称,我想自动获取它们: results = ( { geometry = { location = { lat = "-33.3979227"; lng = "-70.58503859999999"; };

我想提取这些Coffe的名称这是我输出的一个摘录,因为我有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中的
    NSDictionary/NSArray
  • 切勿在Swift中使用
    mutableContainers
    。这完全没用

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

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)")
          }
      }
   }
}

我如何访问geometry-location-lat?这段代码向我展示了lat和lon的n/a,我不知道为什么:(从JSON输出——实际上是字典表示——
lat
lng
似乎是字符串,但可能是数字。你应该发布JSON字符串。我编辑了答案。