如何将字符串添加到参数中

时间:2018-03-08 13:06:42

标签: ios swift4

我想在string类型中添加参数。我正在使用以下方法 添加数据。

 let param = ["user_id": "5","start_date": start,"end_date" : end,"start_time": start_Time,"end_time":end_Time,"departure_time":departure_Time,"payment_email":paymentEmailTextField.text!,"tripData":String1 ,"image": imageData] as [String : Any]

我通过String1

添加“tripData”值
var String1 = """
{
"trip_types":"\(preferedSelectString)",
"attractions": \(addAttractionArray),
"description": "\(self.tripDescriptionTextFiled.text!)",
"type": "\(search_type)",
"cost" : "\(self.priceperPersonTextField.text!)",
"location": "\(self.countryLabel.text!)",
"title": "\(self.tittletripTextField.text!)",
"max_persons": "\(self.peopleCountLabel.text!)",
"advance": "\(self.loadingDepositeTextField.text!)",
"address": "\(self.googleAddressTextField.text!)",
"city": "\(self.cityLabel.text!)",
"state": "\(self.state)",
"country": "\(self.countryLabel.text!)",
"amenities": "\(self.amenitiesString)"
}
"""

刚打印我的控制台string1显示数据

{
"trip_types":"5",
"attractions": [],
"description": "des",
"type": "0",
"cost" : "100",
"location": "India",
"title": "tittle",
"max_persons": "3",
"advance": "50",
"address": "Vadapalani",
"city": "Chennai",
"state": "Tamil Nadu",
"country": "India",
"amenities": "wifi"
}

但是我打印的param值没有正确显示数据

["tripData": "{\n\"trip_types\":\"5\",\n\"attractions\": [],\n\"description\": \"des\",\n\"type\": \"0\",\n\"cost\" : \"100\",\n\"location\": \"India\",\n\"title\": \"tittle\",\n\"max_persons\": \"3\",\n\"advance\": \"50\",\n\"address\": \"Vadapalani\",\n\"city\": \"Chennai\",\n\"state\": \"Tamil Nadu\",\n\"country\": \"India\",\n\"amenities\": \"wi\"\n}", "start_time": "11:00:00", "image": [473044 bytes], "end_time": "10:00:00", "user_id": "5", "payment_email": "add@gmail.com", "start_date": "2018-03-20", "end_date": "2018-03-20", "departure_time": "10:00:00"]

在参数数据\ n \“,\”中,这些值会自动添加。因此,参数中不会添加Json值。

我想显示类型

之后的数据
["tripData": {"trip_types":"1,2,3", "attractions":["something","somecontent"], "description":"Come with me to see","type":"1","cost":"120", "location":"Australia","title":"OLD TOWN CASTLE TOUR", "max_persons":"2","advance":"120", "address":"something","city":"chennai","state":"Tamilnadu","country":"", "amenities":"wifi,photo"},"start_time": "11:00:00", "image": [473044 bytes], "end_time": "10:00:00", "user_id": "5", "payment_email": "add@gmail.com", "start_date": "2018-03-20", "end_date": "2018-03-20", "departure_time": "10:00:00"]

tripData值是字符串格式。 “tripData”在Type(String1)中有一个单独的字符串。如何在字符串中发送参数。请帮帮我!

2 个答案:

答案 0 :(得分:0)

您可以使用JSONEncoder将JSON数据转换为String

    let param = ["user_id": "5"]
    let encoder = JSONEncoder()
    if let jsonData = try? encoder.encode(param) {
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            print(jsonString)
        }
    }

答案 1 :(得分:0)

param["tripData": "{\n\"trip_types\":\"5\",\n\"attractions\": [],\n\"description\": \"des\",\n\"type\": \"0\",\n\"cost\" : \"100\",\n\"location\": \"India\",\n\"title\": \"tittle\",\n\"max_persons\": \"3\",\n\"advance\": \"50\",\n\"address\": \"Vadapalani\",\n\"city\": \"Chennai\",\n\"state\": \"Tamil Nadu\",\n\"country\": \"India\",\n\"amenities\": \"wi\"\n}", "start_time": "11:00:00", "image": [473044 bytes], "end_time": "10:00:00", "user_id": "5", "payment_email": "add@gmail.com", "start_date": "2018-03-20", "end_date": "2018-03-20", "departure_time": "10:00:00"]

这里只添加1个类型为[String:String]的键值。在设置键" tripData"的值之前确保已将JSON字符串转换为字典格式。

您尚未将JSON字符串转换为字典。这就是为什么它将它视为一个字符串 确保在解析之前将String转换为Dictionary。