使用unicode字符解析JSON时出现SwiftyJSON问题

时间:2015-09-01 03:03:15

标签: json swift unicode spree swifty-json

以下是我从API获得的回复

2015-08-31 7:29:45 [GDMNetManagerSMB.swift-228]: response:     Optional(<NSHTTPURLResponse: 0x7fd6c507c6a0> { URL: ... } { status code:     200, headers {
    Age = 0;
    "Cache-Control" = "max-age=0, private, must-revalidate";
    Connection = "keep-alive";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Tue, 01 Sep 2015 02:29:43 GMT";
    Etag = "\"b227c2...28c2034ac\"";
    Server = "ATS/5.2.1";
    Status = "200 OK";
    "Transfer-Encoding" = Identity;
    "X-Content-Type-Options" = nosniff;
    "X-Frame-Options" = SAMEORIGIN;
    "X-Request-Id" = "17f03082-2f44-436a-84f5-7f038e77988f";
    "X-Runtime" = "0.151788";
    "X-UA-Compatible" = "chrome=1";
    "X-XSS-Protection" = "1; mode=block";
} })

然后,在代码中打印json,然后尝试提取“count”和“total_count”

NSLog("sjson :  \(sjson)")

let cnt = sjson["count"]
NSLog("count :  \(cnt)")

let tot_cnt = sjson["total_count"]
NSLog("total_count :  \(tot_cnt)")

这是控制台输出

2015-08-31 19:46:57.416 MyApp[79763:2558418] sjson :      {"count":2,"total_count":2,"current_page":1,"per_page":25,"pages":1,"taxons":[{"taxons":[{"taxons":[{"taxons":[],"id":7,"name":"Burlesque","pretty_name":"Categories -\u003E Modern designs -\u003E Wacko -\u003E Burlesque","permalink":"burlesque","parent_id":5,"taxonomy_id":1,"updated_at":"2015-03-02T22:12:06.000Z"}],"id":5,"name":"Wacko","pretty_name":"Categories -\u003E Modern designs -\u003E Wacko","permalink":"wacko","parent_id":4,"taxonomy_id":1,"updated_at":"20    15-08-22T15:34:07.000Z"}],"id":4,"name":"Modern    designs","pretty_name":"Categories -\u003E Modern designs","permalink":"modern-designs","parent_id":1,"taxonomy_id":1,"updated_at":"2015-08-22T16:03:31.000Z"},{"taxons":[{"taxons":[],"id":6,"name":"Victorian","pretty_name":"Categories -\u003E Classic designs -\u003E Victorian","permalink":"victorian","parent_id":3,"taxonomy_id":1,"updated_at":"2015-06-11T20:07:52.000Z"}],"id":3,"name":"Classic designs","pretty_name":"Categories -\u003E Classic designs","permalink":"classic-designs","parent_id":1,"taxonomy_id":1,"updated_at":"2015-08-22T15:30:18.000Z"}]}
2015-08-31 19:46:57.417 MyApp[79763:2558418] count :  null
2015-08-31 19:46:57.417 MyApp[79763:2558418] total_count :  null

唯一与通常情况不同的是,JSON包含unicode字符,例如

"pretty_name":"Categories -\u003E Modern designs -\u003E Wacko -\u003E Burlesque"

我检查了以下内容:

  • 响应是UTF-8编码

    "Content-Type" = "application/json; charset=utf-8";
    
  • json有效(使用jsonlint.com - 它实际上是Spree端点/ api / taxonomies / 1 / taxons)

      {
    "count": 2,
    "total_count": 2,
    "current_page": 1,
    "per_page": 25,
    "pages": 1,
    "taxons": [
        {
            "taxons": [
                {
                    "taxons": [
                        {
                            "taxons": [],
                            "id": 7,
                            "name": "Burlesque",
                            "pretty_name": "Categories -> Modern designs -> Wacko -> Burlesque",
                            "permalink": "burlesque",
                            "parent_id": 5,
                            "taxonomy_id": 1,
                            "updated_at": "2015-03-02T22:12:06.000Z"
                        }
                    ],
                    "id": 5,
                    "name": "Wacko",
                    "pretty_name": "Categories -> Modern designs -> Wacko",
                    "permalink": "wacko",
                    "parent_id": 4,
                    "taxonomy_id": 1,
                    "updated_at": "2015-08-22T15:34:07.000Z"
                }
            ],
            "id": 4,
            "name": "Modern designs",
            "pretty_name": "Categories -> Modern designs",
            "permalink": "modern-designs",
            "parent_id": 1,
            "taxonomy_id": 1,
            "updated_at": "2015-08-22T16:03:31.000Z"
        },
        {
            "taxons": [
                {
                    "taxons": [],
                    "id": 6,
                    "name": "Victorian",
                    "pretty_name": "Categories -> Classic designs -> Victorian",
                    "permalink": "victorian",
                    "parent_id": 3,
                    "taxonomy_id": 1,
                    "updated_at": "2015-06-11T20:07:52.000Z"
                }
            ],
            "id": 3,
            "name": "Classic designs",
            "pretty_name": "Categories -> Classic designs",
            "permalink": "classic-designs",
            "parent_id": 1,
            "taxonomy_id": 1,
            "updated_at": "2015-08-22T15:30:18.000Z"
        }
    ]
    }
    

这可能是什么问题?

更新:我尝试打印SwiftyJSON错误,这是我得到的:

let cnt = sjson["count"].error
NSLog("count :  \(cnt)")
let tot_cnt = sjson["total_count"].error
NSLog("total_count :  \(tot_cnt)")


2015-09-01 09:24:30.543 SMBAppEnt[84325:2642079] count :  Optional(Error Domain=SwiftyJSONErrorDomain Code=901 "Dictionary["count"] failure, It is not an dictionary" UserInfo=0x7fca2f81f100 {NSLocalizedDescription=Dictionary["count"] failure, It is not an dictionary})
2015-09-01 09:24:30.543 SMBAppEnt[84325:2642079] total_count :  Optional(Error Domain=SwiftyJSONErrorDomain Code=901 "Dictionary["total_count"] failure, It is not an dictionary" UserInfo=0x7fca2cd45d90 {NSLocalizedDescription=Dictionary["total_count"] failure, It is not an dictionary})

1 个答案:

答案 0 :(得分:0)

NSLog("sjson :  \(sjson)")

let cnt = sjson["count"].string
NSLog("count :  \(cnt)")

let tot_cnt = sjson["total_count"].string
NSLog("total_count :  \(tot_cnt)")

OR

NSLog("sjson :  \(sjson)")

let cnt = sjson["count"]!.string!
NSLog("count :  \(cnt)")

let tot_cnt = sjson["total_count"]!.string!
NSLog("total_count :  \(tot_cnt)")