Home > OS >  How to get JSON response without unicode escape sequence Swift
How to get JSON response without unicode escape sequence Swift

Time:11-13

I am trying to get data in correct form, and here is my request example:

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.allHTTPHeaderFields = [
    "Content-Type": "http",
    "Authorization": token
]        
let session = URLSession.shared
session.dataTask(with: request) { data, response, error in

    guard let data = data else {
        completion(.failure(.invalidData))
        return
    }

    do {
        if let string = String(data: data, encoding: .utf8) { print(string) }
        let json = try JSONSerialization.jsonObject(with: data, options: [])
        print(json)
        completion(.failure(.invalidURL))
    }
    catch {
        completion(.failure(.decodingProblem))
    }
}.resume()

After that request I always get the JSON result like the next one. But I would like to get correct text without unicode sequence

{
    "result": {
        "id": 2,
        "write_date": "2021-10-05T08:46:53.945082",
        "name": "\u041e\u0432\u0430\u0434\u0447\u0443\u043a",
        "x_phone": " 380999999999",
        "number_policy": "\u0422\u0435\u0441\u0442",
        "client_email": "[email protected]",
        "x_sk_phone1": " 380999999999",
        "x_manager1": "\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 1",
        "x_u_phone": "45454544",
        "x_u_email": "[email protected]",
        "x_u_phone_man1": "123456789",
        "x_street": "\u0456\u0432\u0441\u044c\u043a\u0430, 29",
        "x_street2": "\u043b\u0456\u0432\u0441\u044c\u043a\u0438\u0439\u00bb, 5 \u044d\u0442\u0430\u0436",
        "x_city": "\u041a\u0438\u0457\u0432",
        "x_com_street": "\\u043e\u0447\u0438\u0446\u044c\u043a\u0430,44",
        "x_ost_zab": "\u0422\u0430\u043a",
        "x_zab_v_obos": "\u0422\u0430\u043a",
        "x_zab_v_rem": "\u041d\u0456",
        "x_travma": "\u0422\u0430\u043a",
        "x_sd_1": "100%",
        "x_sd_2": "\u0422\u0430\u043a",
        "x_sd_3": "\u0422\u0430\u043a",
        "x_sp_1": "\u0422\u0430\u043a, \u0432 \u043c\u0435\u0436\u0430\u0445 \u043b\u0456\u043c\u0456\u0442\u0443",
        "x_dop_1": "\u0422\u0430\u043a",
        "x_info": "\u0422\u0435\u0441\u0442\u043e\u0432\u0438\u0439 \u0437\u0430\u043f\u0438\u0441 \u0434\u043b\u044f \u0432\u0456\u0434\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043d\u044f \u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0456 \u043a\u043b\u0456\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u0431\u0456\u043b\u044c\u043d\u043e\u043c\u0443 \u0434\u043e\u0434\u0430\u0442\u043a\u0443",
        "x_limit_1": "300 000",
        "x_limit_other": "Some"
    }
}

So I would like to get correct text without unicode sequence.

CodePudding user response:

Your JSON string has some issues. When using \U it should send 4 bytes (8 hexa characters) but it is sending only 2 bytes (4 hexa characters). If you have control of what is being returned the easiest solution is to return \u instead of \U which is the correct unicode escape for 2 bytes otherwise you will have to do some string manipulation and apply a string transform before decoding your json:

let jsonData = Data(#"{"x_city":"\U041a\U0438\U0457\U0432"}"#.utf8)
let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
let cleanedData = Data(
    jsonString
    .replacingOccurrences(of: #"\U"#, with: #"\u"#)
    .applyingTransform(.init("Hex-Any"), reverse: false)!
    .utf8
)
struct Response: Codable {
    let xCity: String
}
do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let response = try decoder.decode(Response.self, from: cleanedData)
    
    print(response)  // Response(xCity: "Київ")
} catch {
    print(error)
}
  • Related