SwiftyJSON - 'inout JSON' is not convertible to 'JSON'

走远了吗. 提交于 2019-12-04 05:42:11

问题


I'm facing a JSON parsing issue I've no idea how to fix.

I need this part of the JSON data

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

to be stored in a [String: String]? optional dictionary. So, this is the code I'm using:

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

This however produces a compiler error:

'inout JSON' is not convertible to 'JSON'

I should probably add that this is part of a pretty large piece of JSON data, and this is the only one causing issues.

Any clues would be most appreciated, I'm kind of stuck on this one.


回答1:


Michael, I parse JSON with routine, cannot help but think it is a little more simplistic than yours, but it works :) filesQ.enqueue is an array in essence it adds the fields I want to.

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

Please send me back a copy if you managed to improve it!



来源:https://stackoverflow.com/questions/35598834/swiftyjson-inout-json-is-not-convertible-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!