获取此输出json的所有值

时间:2017-01-29 21:57:04

标签: json swift parsing nsarray nsdictionary

我想提取这些coff的名字,这是我输出的摘录,因为我有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)
                    }

1 个答案:

答案 0 :(得分:0)

一如既往:

    解析JSON时,
  • 从不在Swift中使用public class PersonLogImpl { private boolean remove; private boolean isLicenseUnique; private boolean add; private final ArrayList<Person> person; public PersonLogImpl() { // YOU SHOULD ALSO ASSIGN THE PRIVATE BOOLEANS HERE this.person = new ArrayList<>(); } public ArrayList<Person> getPersonLog() { return Person; // What's going on here? // You're returning the actual Person class when you should be returning // an ArrayList of type Person (I think you meant for the P to be lowercase) } public boolean add(Person obj){ //add person object to ordered list person.add(obj); return add; // You never assign true or false to 'add' (I believe // it defaults to true) so this method will always return true (maybe // false, not sure). you should instead use 'return person.add(obj);' } public boolean remove (String license){ //remove person with specific license from list // The code below makes no sense (no offence). if(person.remove(person.equals(add))){ remove = true; } return remove; // Since I don't know the make up of the 'Person' class I can't // tell you how this method should function. I'll make a guess though: for(int i = 0; i < person.size(); ++i) { if(person.get(i).getLicenseNumber().equals(license)) { return person.remove(i); } } // OR a shorter alternative return person.remove(new Person(license)); } }
  • 从不在Swift中使用NSDictionary / NSArray。这完全没用。

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

results