How to convert JSON to String in ios Swift?

后端 未结 7 1798
孤独总比滥情好
孤独总比滥情好 2020-12-15 16:30

Here is the code for the below Json output:

let params : [[String : AnyObject]]  = [[\"name\" : \"action\", \"value\" : \"pay\" ],[\"name\" : \"cartJsonData\         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 16:41

    Xcode 11, converted String to NSString is working for me.

    func jsonToString(json: AnyObject) -> String{
        do {
            let data1 = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted)
            let convertedString = String(data: data1, encoding: String.Encoding.utf8) as NSString? ?? ""
            debugPrint(convertedString)
            return convertedString as String
        } catch let myJSONError {
            debugPrint(myJSONError)
            return ""
        }
    }
    

提交回复
热议问题