简体   繁体   English

Swift Alamofire转义JSON

[英]Swift Alamofire Escape a JSON

I am working with swift Alamofire, but i had an issue, i want pass data to the server but the server required escaped JSON string, like this 我正在使用迅速的Alamofire,但遇到一个问题,我想将数据传递到服务器,但是服务器需要转义的JSON字符串,例如

{\"status\":1,\"id\":\"1bcc3331b09d32f7439ad9d5f2acfb35\",\"progress\":[{\"airline_code\":\"SRI\",\"ctr\":0,\"rate\":5,\"state\":1,\"data\":null}]}

but I have this data from the response: 但我从响应中得到了以下数据:

{"status":1,"id":"1bcc3331b09d32f7439ad9d5f2acfb35","progress":[{"airline_code":"SRI","ctr":0,"rate":5,"state":1,"data":null}]}

how can I convert JSON to escaped JSON like that, 我该如何将JSON转换为转义的JSON,

I have tried this online converter Converter Escape JSON 我已经尝试过此在线转换器Converter Escape JSON

and its works perfect, how can I escape like that in swift Xcode. 并且它的作品非常完美,我该如何在迅捷的Xcode中逃脱。

update complete json (look inside feed: ) 更新完整的json(在feed中查看:)

{
   "flight_child":"0",
   "child":"[]",
   "depart_hidden_transit":"0",
   "depart_adult_surcharge":"0",
   "depart_flight":"[{\"airlineCode\":\"LIO\",\"arriveCity\":\"Jakarta Soekarno Hatta\",\"arriveDate\":\"2018-04-28\",\"arriveDatetime\":\"2018-04-28 20:05\",\"arrivePort\":\"CGK\",\"arriveTime\":\"20:05\",\"arriveTimezone\":7.0,\"departCity\":\"Yogyakarta\",\"departDate\":\"2018-04-28\",\"departDatetime\":\"2018-04-28 18:50\",\"departPort\":\"JOG\",\"departTime\":\"18:50\",\"departTimezone\":7.0,\"flightNumber\":\"JT 555\",\"stopCount\":0}]",
   "auth_mode":"",
   "flight_infant":"0",
   "flight_to":"CGK",
   "flight_return":"",
   "depart_child_discount":"0",
   "infant":"[]",
   "depart_choice":"d3ea46551c769f462c3e1a4dd25c933d",
   "depart_child_surcharge":"0",
   "version_code":"3",
   "contact_email":"mul@gmai.oc",
   "contact_name":"Mulia RIfai",
   "adult":"[[\"Mr\",\"Muhammad Fuad\",null,null,\"0\",null,null,null,null,null]]",
   "flight_trip":"oneway",
   "flight_adult":"1",
   "depart_adult_discount":"0",
   "contact_phone":"0972312",
   "depart_carrier":"LIO",
   "client_password":"arena123",
   "depart_infant_discount":"0",
   "depart_infant_surcharge":"0",
   "client_username":"androidarena",
   "flight_depart":"2018-05-03",
   "feed":"{\"status\":1,\"id\":\"1bcc3331b09d32f7439ad9d5f2acfb35\",\"progress\":[{\"airline_code\":\"SRI\",\"ctr\":0,\"rate\":5,\"state\":1,\"data\":null}]}",
   "contact_title":"Mr.",
   "flight_from":"JOG",
   "depart_class":"eco",
   "device_id":"123456789011123"
}

Even though this should be fixed by the server. 即使这应该由服务器修复。 A nasty hack would be to first stringify your json that should be passed as the string and then replacing the occurrence of double quotation with \\". 讨厌的破解方法是首先将应该作为字符串传递的json字符串化,然后将\\替换为双引号。

let dic = ["status":1,
               "id":"1bcc3331b09d32f7439ad9d5f2acfb35",
               "progress":[["airline_code":"SRI","ctr":0,"rate":5,"state":1,"data":nil]
    ]

        ] as [String : Any]

    if let json = try? JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions.init(rawValue: 0)) {
        if let str = String(data: json, encoding: String.Encoding.utf8)?.replacingOccurrences(of: "\"", with: "\\\"") {
            print(str)
        }
    }

Result: 结果:

{\"id\":\"1bcc3331b09d32f7439ad9d5f2acfb35\",\"status\":1,\"progress\":[{\"state\":1,\"airline_code\":\"SRI\",\"data\":null,\"ctr\":0,\"rate\":5}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM