How to convert a JSON string to a dictionary?

前端 未结 10 903
情深已故
情深已故 2020-11-22 05:57

I want to make one function in my swift project that converts String to Dictionary json format but I got one error:

Cannot convert expression\'s type

相关标签:
10条回答
  • 2020-11-22 06:38

    Warning: this is a convenience method to convert a JSON string to a dictionary if, for some reason, you have to work from a JSON string. But if you have the JSON data available, you should instead work with the data, without using a string at all.

    Swift 3

    func convertToDictionary(text: String) -> [String: Any]? {
        if let data = text.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            } catch {
                print(error.localizedDescription)
            }
        }
        return nil
    }
    
    let str = "{\"name\":\"James\"}"
    
    let dict = convertToDictionary(text: str)
    

    Swift 2

    func convertStringToDictionary(text: String) -> [String:AnyObject]? {
        if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
            do {
                return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }
    
    let str = "{\"name\":\"James\"}"
    
    let result = convertStringToDictionary(str)
    

    Original Swift 1 answer:

    func convertStringToDictionary(text: String) -> [String:String]? {
        if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
            var error: NSError?
            let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error) as? [String:String]
            if error != nil {
                println(error)
            }
            return json
        }
        return nil
    }
    
    let str = "{\"name\":\"James\"}"
    
    let result = convertStringToDictionary(str) // ["name": "James"]
    
    if let name = result?["name"] { // The `?` is here because our `convertStringToDictionary` function returns an Optional
        println(name) // "James"
    }
    

    In your version, you didn't pass the proper parameters to NSJSONSerialization and forgot to cast the result. Also, it's better to check for the possible error. Last note: this works only if your value is a String. If it could be another type, it would be better to declare the dictionary conversion like this:

    let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error) as? [String:AnyObject]
    

    and of course you would also need to change the return type of the function:

    func convertStringToDictionary(text: String) -> [String:AnyObject]? { ... }
    
    0 讨论(0)
  • 2020-11-22 06:39

    With Swift 3, JSONSerialization has a method called json​Object(with:​options:​). json​Object(with:​options:​) has the following declaration:

    class func jsonObject(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> Any
    

    Returns a Foundation object from given JSON data.

    When you use json​Object(with:​options:​), you have to deal with error handling (try, try? or try!) and type casting (from Any). Therefore, you can solve your problem with one of the following patterns.


    #1. Using a method that throws and returns a non-optional type

    import Foundation
    
    func convertToDictionary(from text: String) throws -> [String: String] {
        guard let data = text.data(using: .utf8) else { return [:] }
        let anyResult: Any = try JSONSerialization.jsonObject(with: data, options: [])
        return anyResult as? [String: String] ?? [:]
    }
    

    Usage:

    let string1 = "{\"City\":\"Paris\"}"
    do {
        let dictionary = try convertToDictionary(from: string1)
        print(dictionary) // prints: ["City": "Paris"]
    } catch {
        print(error)
    }
    
    let string2 = "{\"Quantity\":100}"
    do {
        let dictionary = try convertToDictionary(from: string2)
        print(dictionary) // prints [:]
    } catch {
        print(error)
    }
    
    let string3 = "{\"Object\"}"
    do {
        let dictionary = try convertToDictionary(from: string3)
        print(dictionary)
    } catch {
        print(error) // prints: Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 9." UserInfo={NSDebugDescription=No value for key in object around character 9.}
    }
    

    #2. Using a method that throws and returns an optional type

    import Foundation
    
    func convertToDictionary(from text: String) throws -> [String: String]? {
        guard let data = text.data(using: .utf8) else { return [:] }
        let anyResult: Any = try JSONSerialization.jsonObject(with: data, options: [])
        return anyResult as? [String: String]
    }
    

    Usage:

    let string1 = "{\"City\":\"Paris\"}"
    do {
        let dictionary = try convertToDictionary(from: string1)
        print(String(describing: dictionary)) // prints: Optional(["City": "Paris"])
    } catch {
        print(error)
    }
    
    let string2 = "{\"Quantity\":100}"
    do {
        let dictionary = try convertToDictionary(from: string2)
        print(String(describing: dictionary)) // prints nil
    } catch {
        print(error)
    }
    
    let string3 = "{\"Object\"}"
    do {
        let dictionary = try convertToDictionary(from: string3)
        print(String(describing: dictionary))
    } catch {
        print(error) // prints: Error Domain=NSCocoaErrorDomain Code=3840 "No value for key in object around character 9." UserInfo={NSDebugDescription=No value for key in object around character 9.}
    }
    

    #3. Using a method that does not throw and returns a non-optional type

    import Foundation
    
    func convertToDictionary(from text: String) -> [String: String] {
        guard let data = text.data(using: .utf8) else { return [:] }
        let anyResult: Any? = try? JSONSerialization.jsonObject(with: data, options: [])
        return anyResult as? [String: String] ?? [:]
    }
    

    Usage:

    let string1 = "{\"City\":\"Paris\"}"
    let dictionary1 = convertToDictionary(from: string1)
    print(dictionary1) // prints: ["City": "Paris"]
    
    let string2 = "{\"Quantity\":100}"
    let dictionary2 = convertToDictionary(from: string2)
    print(dictionary2) // prints: [:]
    
    let string3 = "{\"Object\"}"
    let dictionary3 = convertToDictionary(from: string3)
    print(dictionary3) // prints: [:]
    

    #4. Using a method that does not throw and returns an optional type

    import Foundation
    
    func convertToDictionary(from text: String) -> [String: String]? {
        guard let data = text.data(using: .utf8) else { return nil }
        let anyResult = try? JSONSerialization.jsonObject(with: data, options: [])
        return anyResult as? [String: String]
    }
    

    Usage:

    let string1 = "{\"City\":\"Paris\"}"
    let dictionary1 = convertToDictionary(from: string1)
    print(String(describing: dictionary1)) // prints: Optional(["City": "Paris"])
    
    let string2 = "{\"Quantity\":100}"
    let dictionary2 = convertToDictionary(from: string2)
    print(String(describing: dictionary2)) // prints: nil
    
    let string3 = "{\"Object\"}"
    let dictionary3 = convertToDictionary(from: string3)
    print(String(describing: dictionary3)) // prints: nil
    
    0 讨论(0)
  • 2020-11-22 06:40

    Swift 3:

    if let data = text.data(using: String.Encoding.utf8) {
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
            print(json)
        } catch {
            print("Something went wrong")
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:49

    I've updated Eric D's answer for Swift 5:

     func convertStringToDictionary(text: String) -> [String:AnyObject]? {
        if let data = text.data(using: .utf8) {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:AnyObject]
                return json
            } catch {
                print("Something went wrong")
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-11-22 06:51

    Swift 5

    extension String {
        func convertToDictionary() -> [String: Any]? {
            if let data = data(using: .utf8) {
                return try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            }
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:56

    Swift 4

    extension String {
        func convertToDictionary() -> [String: Any]? {
            if let data = self.data(using: .utf8) {
                do {
                    return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                } catch {
                    print(error.localizedDescription)
                }
            }
            return nil
        }
    }
    
    0 讨论(0)
提交回复
热议问题