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

标签 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})

最佳答案

NSLog("sjson :  \(sjson)")

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

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

或者

NSLog("sjson :  \(sjson)")

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

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

关于json - 使用 unicode 字符解析 JSON 时出现 SwiftyJSON 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322881/

相关文章:

javascript - 以 Angular 形式显示订阅响应值

Java字符串修剪没有效果

统一码编码

python - 如何将 JSON 转储从表单填充到 Django 模板?

json - 从 JSON 数据向 TableView 添加部分

ios - 根据选择的初始值显示 JSON 数据

swift - 删除 CollectionView iOS 中的间距

ios - Rx swift : Complex TextFields Validation

ios - 包含在 Swift 中放置在自己的方法中时语法不同的地方

c - 在 C 中读取 Unicode 文件并通过套接字将内容作为 ASCII 传递