简体   繁体   English

JSON键:原始JSON调用后值对消失

[英]JSON key:value pairs disappearing after original json call

I have been trying to read data from JSON using Swift, but it hasn't been working after the original json call. 我一直在尝试使用Swift从JSON读取数据,但是在原始的json调用之后它一直无法正常工作。 Here is my code that does work: 这是我的代码,可以正常工作:

func loadUrl(url:String){
    DispatchQueue.global().async {
        do {
            let appUrl = URL(string:url)!
            let data = try Data(contentsOf:appUrl)
            let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
            DispatchQueue.main.async {
                print(json)
            }
        } catch {
            DispatchQueue.main.async {
                print("Cannot connect to the server.")
            }
        }
    }
}

It prints this result: 它显示以下结果:

["results": <__NSSingleObjectArrayI 0x60000000ae80>(
    {
        "address_components" =     (
                {
            "long_name" = Brooklyn;
            "short_name" = Brooklyn;
            types =             (
                political,
                sublocality,
                "sublocality_level_1"
            );
        },
                {
            "long_name" = "New York";
            "short_name" = "New York";
            types =             (
                locality,
                political
            );
        },
                {
            "long_name" = "Kings County";
            "short_name" = "Kings County";
            types =             (
                "administrative_area_level_2",
                political
            );
        },
                {
            "long_name" = "New York";
            "short_name" = NY;
            types =             (
                "administrative_area_level_1",
                political
            );
        },
                {
            "long_name" = "United States";
            "short_name" = US;
            types =             (
                country,
                political
            );
        }
    );
    "formatted_address" = "Brooklyn, NY, USA";
    geometry =     {
        bounds =         {
            northeast =             {
                lat = "40.739446";
                lng = "-73.83336509999999";
            };
            southwest =             {
                lat = "40.551042";
                lng = "-74.05663";
            };
        };
        location =         {
            lat = "40.6781784";
            lng = "-73.94415789999999";
        };
        "location_type" = APPROXIMATE;
        viewport =         {
            northeast =             {
                lat = "40.739446";
                lng = "-73.83336509999999";
            };
            southwest =             {
                lat = "40.551042";
                lng = "-74.05663";
            };
        };
    };
    "place_id" = ChIJCSF8lBZEwokRhngABHRcdoI;
    types =     (
        political,
        sublocality,
        "sublocality_level_1"
    );
}
)
, "status": OK]

However, what I would like to do is go all the way to the formatted_address key. 但是,我想做的就是一直到formatted_address键。 As soon as I try this code, however, the program crashes: 但是,一旦尝试此代码,程序就会崩溃:

func loadUrl(url:String){
    DispatchQueue.global().async {
        do {
            let appUrl = URL(string:url)!
            let data = try Data(contentsOf:appUrl)
            let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
            let results = json["results"] as! [String:Any]
            DispatchQueue.main.async {
                print(results)
            }
        } catch {
            DispatchQueue.main.async {
                print("Cannot connect to the server.")
            }
        }
    }
}

Does anybody know how to fix this? 有人知道如何解决此问题吗? The only thing that I have noticed is that the value of results is: 我唯一注意到的是结果的价值是:

<__NSSingleObjectArrayI 0x604000206d60>

I have no clue what this is. 我不知道这是什么。 When I put the same json into an online viewer, the value was simply 0. Why is this? 当我将相同的json放入在线查看器中时,该值仅为0。这是为什么? Shouldn't the value of "results" be address_components, formatted_address, geometry, place_id, and types? “结果”的值不应该是address_components,formatted_address,geometry,place_id和types吗?

Can you try 你能试一下吗

if let results = json["results"] as? [[String:Any]] {

   if let first = results[0] as? [String:Any] {
          print(first["formatted_address"])
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM