美文网首页
Swift 字典转JSON字符串

Swift 字典转JSON字符串

作者: 三少爷_贱 | 来源:发表于2017-11-17 15:58 被阅读0次

///封装JSON字符串

func GetJson(){

//判断是否使用积分

var dictionaryOrArray : [String: AnyObject] = [:]

dictionaryOrArray["orderType"] = 0 as AnyObject?

dictionaryOrArray["remark"] = "Hello Swift" as AnyObject?

dictionaryOrArray["consume"] = 100as AnyObject?

dictionaryOrArray["orderIsintegral"] = 1 as AnyObject?

dictionaryOrArray["orderInteralamount"] = 100 as AnyObject?

dictionaryOrArray["couponId"] = 34 as AnyObject?

dictionaryOrArray["orderAmount"] = 345 as AnyObject?

dictionaryOrArray["couponAmount"] =10 as AnyObject?

dictionaryOrArray["realAmount"] = 234 as AnyObject?

dictionaryOrArray["orderFreight"] = 12.25 as AnyObject?

dictionaryOrArray["dealBy"] = 2 as AnyObject?

dictionaryOrArray["addrId"] = 12 as AnyObject?

var dataOrArray = [[String:AnyObject]]()

var i = 0

while (i < stringTest.count) {

var dataInfo : [String: AnyObject] = [:]

dataInfo["commodId"] = stringTest[i].GoodsID as AnyObject?

dataInfo["commodNum"] = stringTest[i].CommodCount as AnyObject?

dataOrArray.append(dataInfo)

i = i + 1

}

dictionaryOrArray["data"] = dataOrArray as AnyObject?

let convertResult:String = tool.convertDictionaryToString(dict: dictionaryOrArray)

print("字典转换之后的字符串:\(convertResult)")

}

相关文章

网友评论

      本文标题:Swift 字典转JSON字符串

      本文链接:https://www.haomeiwen.com/subject/mbujvxtx.html