How to convert JSON to String in ios Swift?

后端 未结 7 1776
孤独总比滥情好
孤独总比滥情好 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:55

    Swift (as of December 3, 2020)

    func jsonToString(json: AnyObject){
        do {
            let data1 =  try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
            let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string
            print(convertedString ?? "defaultvalue")
        } catch let myJSONError {
            print(myJSONError)
        }
        
    }
    

提交回复
热议问题