【问题标题】:SwiftyJSON - 'inout JSON' is not convertible to 'JSON'SwiftyJSON - 'inout JSON' 不能转换为 'JSON'
【发布时间】:2016-02-24 09:59:34
【问题描述】:

我遇到了一个 JSON 解析问题,我不知道如何解决。

我需要这部分 JSON 数据

"columns": { "created_at": "DESC", "id": "DESC" }

存储在[String: String]? 可选字典中。所以,这是我正在使用的代码:

self.columns = json["columns"].dictionary?.map { 
(key, value) -> (String, String) in
            return (key, value.stringValue)
}

但这会产生编译器错误:

'inout JSON' 不能转换为 'JSON'

我可能应该补充一点,这是一个相当大的 JSON 数据的一部分,这是唯一引起问题的一个。

任何线索都将不胜感激,我有点坚持这个。

【问题讨论】:

  • Michal,您是否查看了它试图解析的内容以弄清楚为什么它无法解析它? print("self.columns") 并发布。它显然不符合 JSON 对象,那么它是什么?
  • @user3069232 这是从对该值的请求中返回的内容:"columns": { "created_at": "DESC", "id": "DESC" } 可能应该将其添加到我的原始帖子中,我现在就这样做。

标签: ios json xcode swift swifty-json


【解决方案1】:

Michael,我用例程解析 JSON,不禁认为它比你的更简单,但它有效 :) filesQ.enqueue 本质上是一个数组,它添加了我想要的字段。

func jsonParser(json2parse: AnyObject, field2file: String) -> Int {

    if (json2parse is NSDictionary) {
        for (key,value) in json2parse as! NSDictionary {
            switch (value) {
            case is NSDictionary:
                self.jsonParser(value as! NSDictionary, field2file: field2file)
                break
            case is NSArray:
                self.jsonParseArray(value as! NSArray, field2file: field2file)
                break
            case is NSString:
                parsedJson[key as! String] = value
                if (key as! String == field2file) {
                    let file2file = self.parsedJson[field2file] as? String
                    filesQ.enqueue("ignore", theFile: file2file!)
                }
                break
            default:
                break
            }
        }
    }
    return(filesQ.qcount())
}

func jsonParseArray(json2parse: AnyObject, field2file: String) {
    for (item) in json2parse as! NSArray {
        self.jsonParser(item, field2file: field2file)
    }
}

如果你设法改进它,请给我发回一份!

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2016-08-12
    相关资源
    最近更新 更多