ios - Swift Alamofire 转义 JSON

标签 ios json swift alamofire

我正在使用 swift Alamofire,但我遇到了一个问题,我想将数据传递到服务器,但服务器需要转义 JSON 字符串,如下所示

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

但我从响应中得到了以下数据:

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

如何将 JSON 转换为转义 JSON,

我尝试过这个在线转换器 Converter Escape JSON

而且它的工作完美,我怎样才能在 swift Xcode 中像这样逃脱。

更新完整的 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"
}

最佳答案

即使这应该由服务器修复。一个令人讨厌的 hack 是首先对应该作为字符串传递的 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)
        }
    }

结果:

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

关于ios - Swift Alamofire 转义 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097519/

相关文章:

swift - 没有类型注释的参数 swift

ios - 以编程方式工具栏项目不显示 iOS swift 4

ios - 将扩展标记为 @objc 仅用于单元测试

iphone - 如何在 iOS 中制作可 ScrollView ?

ios - 如何在推送 View 之前加载 PDF

java - 嵌套 Json 以使用 Jackson 映射

javascript - Access-Control-Allow-Origin 不允许 XMLHttpRequest 无法加载

python - R 或 Python 中多个 json 文件的数据帧

ios - CGRect 放大动画在 Xcode 10 中不起作用?

ios - 通过原生 iOS 应用使用 Devise 创建用户帐户