我需要從REST API創建發送一個JSON創建JSON:斯威夫特API REST
{
"ownId": "seu_identificador_proprio",
"amount": {
"currency": "BRL",
"subtotals": {
"shipping": 1000
}
},
"items": [
{
"product": "Descrição do pedido",
"quantity": 1,
"detail": "Mais info...",
"price": 1000
}
],
"customer": {
"ownId": "seu_identificador_proprio_de_cliente",
"fullname": "Jose Silva",
"email": "[email protected]",
"birthDate": "1988-12-30",
"taxDocument": {
"type": "CPF",
"number": "22222222222"
},
"phone": {
"countryCode": "55",
"areaCode": "11",
"number": "66778899"
},
"shippingAddress": {
"street": "Avenida Faria Lima",
"streetNumber": 2927,
"complement": 8,
"district": "Itaim",
"city": "Sao Paulo",
"state": "SP",
"country": "BRA",
"zipCode": ""
}
}
}
我感到困惑與創建..
我試着開始[NSObject:AnyObject]
var d1 : [NSObject:AnyObject] = ["ownId":"seu_identificador_proprio", "customer":""]
let dd1 = ["currency":"BRL"]
let dd2 = ["shipping":"1000"]
let arr = [d1]
let d = try! NSJSONSerialization.dataWithJSONObject(arr, options: NSJSONWritingOptions.PrettyPrinted)
let s = NSString(data: d, encoding: NSUTF8StringEncoding)! as String
print(s)
但我需要幫助!
您使用SWIFT 3? – dirtydanee
沒有,SWIFT 2.3 @dirtydanee –
那麼什麼是你的問題?你發佈的代碼有什麼問題? (順便說一句,沒有理由不是測試你的JSON輸出轉換從NSData的一個字符串等,也沒有理由使用漂亮的打印格式。爲了發送到一個RESTful的服務器,你不應該用漂亮的格式。) –