Convert Json string to Json object in Swift 4

后端 未结 4 1190
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 06:13

I try to convert JSON string to a JSON object but after JSONSerialization the output is nil in JSON.

Response String:

[{\\\         


        
相关标签:
4条回答
  • 2020-12-08 06:42

    I tried the solutions here, and as? [String:AnyObject] worked for me:

    do{
        if let json = stringToParse.data(using: String.Encoding.utf8){
            if let jsonData = try JSONSerialization.jsonObject(with: json, options: .allowFragments) as? [String:AnyObject]{
                let id = jsonData["id"] as! String
                ...
            }
        }
    }catch {
        print(error.localizedDescription)
    
    }
    
    0 讨论(0)
  • 2020-12-08 06:48

    The problem is that you thought your jsonString is a dictionary. It's not.

    It's an array of dictionaries. In raw json strings, arrays begin with [ and dictionaries begin with {.


    I used your json string with below code :

    let string = "[{\"form_id\":3465,\"canonical_name\":\"df_SAWERQ\",\"form_name\":\"Activity 4 with Images\",\"form_desc\":null}]"
    let data = string.data(using: .utf8)!
    do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]
        {
           print(jsonArray) // use the json here     
        } else {
            print("bad json")
        }
    } catch let error as NSError {
        print(error)
    }
    

    and I am getting the output :

    [["form_desc": <null>, "form_name": Activity 4 with Images, "canonical_name": df_SAWERQ, "form_id": 3465]]
    
    0 讨论(0)
  • 2020-12-08 06:54

    Using JSONSerialization always felt unSwifty and unwieldy, but it is even more so with the arrival of Codable in Swift 4. If you wield a [String:Any] in front of a simple struct it will ... hurt. Check out this in a Playground:

    import Cocoa
    
    let data = "[{\"form_id\":3465,\"canonical_name\":\"df_SAWERQ\",\"form_name\":\"Activity 4 with Images\",\"form_desc\":null}]".data(using: .utf8)!
    
    struct Form: Codable {
        let id: Int
        let name: String
        let description: String?
    
        private enum CodingKeys: String, CodingKey {
            case id = "form_id"
            case name = "form_name"
            case description = "form_desc"
        }
    }
    
    do {
        let f = try JSONDecoder().decode([Form].self, from: data)
        print(f)
        print(f[0])
    } catch {
        print(error)
    }
    

    With minimal effort handling this will feel a whole lot more comfortable. And you are given a lot more information if your JSON does not parse properly.

    0 讨论(0)
  • 2020-12-08 06:59

    I used below code and it's working fine for me. :

    let jsonText = "{\"userName\":\"Bhavsang\"}"
    var dictonary:NSDictionary?
        
    if let data = jsonText.dataUsingEncoding(NSUTF8StringEncoding) {
            
         do {
                dictonary =  try NSJSONSerialization.JSONObjectWithData(data, options: [.allowFragments]) as? [String:AnyObject]
            
                if let myDictionary = dictonary
                  {
                      print(" User name is: \(myDictionary["userName"]!)")
                  }
                } catch let error as NSError {
                
                print(error)
             }
    }
    
    0 讨论(0)
提交回复
热议问题