Swift with Nested Json - 访问非字符串的密钥

时间:2015-11-18 20:35:54

标签: ios json swift

我正在尝试在Swift中访问我的API请求(嵌套Json)中的密钥,但我遇到的问题是NSDictionary中的一些密钥是Strings {{1}并且某些键没有"。没有"(不是字符串)的密钥我无法访问。

我希望将"中的所有密钥设为NSDictionaryString,以便我可以访问数据。

API服务器的输出。

API数据为JSON Sting

NSString

将API数据转换为{"id":1,"first_name":"joe","last_name":"blow","email":"joeblow@test.com","api_authtoken":"qtgfLxuzaEJoJoLkZjdWt","authtoken_expiry":"2015-11-19T06:12:45.000Z","avatar_file_name":"1-1447697130.jpg","avatar_content_type":"image/png","avatar_file_size":516066,"avatar_updated_at":"2015-11-16T18:05:30.000Z","uid":null,"latitude":null,"longitude":null,"address":null,"city":null,"state":null,"country":null,"postal_code":null,"phone_number":null,"belt":null,"fbid":null,"badges":[{"id":1,"user_id":1,"badge_type_id":1,"created_at":"2015-11-18T17:34:43.000Z","updated_at":"2015-11-18T17:34:43.000Z","badge_url":"http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge1.png"},{"id":2,"user_id":1,"badge_type_id":2,"created_at":"2015-11-18T17:34:43.000Z","updated_at":"2015-11-18T17:34:43.000Z","badge_url":"http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge2.png"},{"id":3,"user_id":1,"badge_type_id":3,"created_at":"2015-11-18T17:34:43.000Z","updated_at":"2015-11-18T17:34:43.000Z","badge_url":"http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge3.png"},{"id":4,"user_id":1,"badge_type_id":4,"created_at":"2015-11-18T17:34:43.000Z","updated_at":"2015-11-18T17:34:43.000Z","badge_url":"http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge4.png"}]}

NSDictionary

相应的let responseDict = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary 如下:

NSDictionary

}

我想做的是在json中迭代“徽章”数据。

我像这样访问徽章数据:

PRINTING NSDICTIONARY = {
address = "<null>";
"api_authtoken" = qtgfLxuzaEJoJoLkZjdWt;
"authtoken_expiry" = "2015-11-19T06:12:45.000Z";
"avatar_content_type" = "image/png";
"avatar_file_name" = "1-1447697130.jpg";
"avatar_file_size" = 516066;
"avatar_updated_at" = "2015-11-16T18:05:30.000Z";
badges =     (
            {
        "badge_type_id" = 1;
        "badge_url" = "http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge1.png";
        "created_at" = "2015-11-18T17:34:43.000Z";
        id = 1;
        "updated_at" = "2015-11-18T17:34:43.000Z";
        "user_id" = 1;
    },
            {
        "badge_type_id" = 2;
        "badge_url" = "http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge2.png";
        "created_at" = "2015-11-18T17:34:43.000Z";
        id = 2;
        "updated_at" = "2015-11-18T17:34:43.000Z";
        "user_id" = 1;
    },
            {
        "badge_type_id" = 3;
        "badge_url" = "http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge3.png";
        "created_at" = "2015-11-18T17:34:43.000Z";
        id = 3;
        "updated_at" = "2015-11-18T17:34:43.000Z";
        "user_id" = 1;
    },
            {
        "badge_type_id" = 4;
        "badge_url" = "http://newappy-dev.s3-us-west-1.amazonaws.com/badges/badge4.png";
        "created_at" = "2015-11-18T17:34:43.000Z";
        id = 4;
        "updated_at" = "2015-11-18T17:34:43.000Z";
        "user_id" = 1;
    }
);
belt = "<null>";
city = "<null>";
country = "<null>";
email = "joeblow@test.com";
fbid = "<null>";
"first_name" = joe;
id = 1;
"last_name" = blow;
latitude = "<null>";
longitude = "<null>";
"phone_number" = "<null>";
"postal_code" = "<null>";
state = "<null>";
uid = "<null>";

但是我没有得到数据。我在这做错了什么?谢谢!

2 个答案:

答案 0 :(得分:2)

您的badges键包含一系列词典,因此您应该使用:

let badges = dictionary.objectForKey("badges") as? [[String:AnyObject]]

由于objectForKey:返回AnyObject?,我们可以使用它来构建字典数组,其中String为关键字,AnyObject为值。获得数组后,您可以遍历它并获得所需的值。

我建议从Objective C NSDictionary类型转换为Swift native Dictionary。这将使您能够以更轻松的方式使用下标并处理您的代码。

答案 1 :(得分:2)

说比我快几分钟。我只是写了如何实现它的例子:

Swift 1.2

if let
    jsonData = NSData(contentsOfFile: jsonPath),
    json = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: nil) as? [String: AnyObject]
{
    if let badges = json["badges"] as? [AnyObject]{
        for badge in badges{
            println(badge)
        }

    }
}

Swift 2.0

let path = NSBundle.mainBundle().bundlePath as NSString // it needs be NSString, Swift String doesn't have stringByAppendingPathComponent method
let jsonPath = path.stringByAppendingPathComponent("data.json")


do{
    if let jsonData = NSData(contentsOfFile: jsonPath){
        let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options:NSJSONReadingOptions.AllowFragments)
        if let badges = json["badges"] as? [AnyObject]{
            for badge in badges{
                print(badge)
                //or process it in any other way
            }

        }

    }
}catch let error as NSError {
    print(error)
}