Reputation: 1587
I got the following encoded string in my web service response
\U00e0\U00aa\U0095\U00e0\U00ab\U0083\U00e0\U00aa\U00aa\U00e0\U00aa\U00be \U00e0\U00aa\U0095\U00e0\U00aa\U00b0\U00e0\U00ab\U0080\U00e0\U00aa\U00a8\U00e0\U00ab\U0087 \U00e0\U00aa\U009f\U00e0\U00ab\U008b\U00e0\U00aa\U00a1 \U00e0\U00aa\U008f\U00e0\U00aa\U00aa\U00e0\U00ab\U008d\U00e0\U00aa\U00b2\U00e0\U00aa\U00bf\U00e0\U00aa\U0095\U00e0\U00ab\U0087\U00e0\U00aa\U00b6\U00e0\U00aa\U00a8 \U00e0\U00aa\U00a4\U00e0\U00aa\U00aa\U00e0\U00aa\U00be\U00e0\U00aa\U00b8\U00e0\U00ab\U008b. \U00e0\U00aa\U00a4\U00e0\U00ab\U008d\U00e0\U00aa\U00af\U00e0\U00aa\U00be\U00e0\U00aa\U0082 \U00e0\U00aa\U00aa\U00e0\U00ab\U008d\U00e0\U00aa\U00b0\U00e0\U00aa\U00b5\U00e0\U00aa\U00be\U00e0\U00aa\U00b8 \U00e0\U00aa\U00ae\U00e0\U00aa\U00be\U00e0\U00aa\U009f\U00e0\U00ab\U0087 \U00e0\U00aa\U008f\U00e0\U00aa\U0095 \U00e0\U00aa\U00a8\U00e0\U00aa\U00b5\U00e0\U00ab\U0080 \U00e0\U00aa\U00a4\U00e0\U00aa\U00aa\U00e0\U00aa\U00be\U00e0\U00aa\U00b8 \U00e0\U00aa\U009b\U00e0\U00ab\U0087
and also
àªà«àªªàª¾ àªàª°à«àª¨à« àªà«àª¡ àªàªªà«àªàªªà«àª\302²àª¿àªàªªà«àª\302²àª¿àª\303à«àª¶àª¨ તપાàપà«àª\302²àª¿àª\303à«àª¶àª¨ તપાà\302ª¸à«. તà«àª¯àª¾àª પà«àª°àªµàª¾àª¸ માàªà« àªàª નવૠતપાસ àªà«.
so how can i decode both the string in swift 3.
got following object in json(Postman Output)
{
"message": "કૃપા કરીને એપ્લિકેશન તપાસો. ત્યાં પ્રવાસ માટે એક નવી તપાસ છે.",
},
{
"message": "કૃપા કરીને એપ્લિકેશન તપાસો. ત્યાં પ્રવાસ માટે એક નવી તપાસ છે.",
}
but in xcode i got following
"Notifications" : [
{
"message" : "àªà«àªªàª¾ àªàª°à«àª¨à« àªà«àª¡ àªàªªà«àªàªªà«àª\302²àª¿àªàªªà«àª\302²àª¿àª\303à«àª¶àª¨ તપાàપà«àª\302²àª¿àª\303à«àª¶àª¨ તપાà\302ª¸à«. તà«àª¯àª¾àª પà«àª°àªµàª¾àª¸ માàªà« àªàª નવૠતપાસ àªà«.",
},
{
"message" : "àªà«àªªàª¾ àªàª°à«àª¨à« àªà«àª¡ àªàªªà«àª²àª¿àªà«àª¶àª¨ તપાસà«. તà«àª¯àª¾àª પà«àª°àªµàª¾àª¸ માàªà« àªàª નવૠતપાસ àªà«.",
}
Upvotes: 2
Views: 868
Reputation: 437872
The problem is that you're just printing the nested NSArray
/NSDictionary
objects, which does not properly render the UTF8 strings:
let originalJsonObject = try? JSONSerialization.jsonObject(with: data)
print("\(originalJsonObject)") // this will show the cryptic \U00ee\U00a0 ... result
That will show text much like you shared in your question:
...
message = "\U0a95\U0ac3\U0aaa\U0abe \U0a95\U0ab0\U0ac0\U0aa8\U0ac7 \U0a9f\U0acb\U0aa1 \U0a8f\U0aaa\U0acd\U0ab2\U0abf\U0a95\U0ac7\U0ab6\U0aa8 \U0aa4\U0aaa\U0abe\U0ab8\U0acb. \U0aa4\U0acd\U0aaf\U0abe\U0a82 \U0aaa\U0acd\U0ab0\U0ab5\U0abe\U0ab8 \U0aae\U0abe\U0a9f\U0ac7 \U0a8f\U0a95 \U0aa8\U0ab5\U0ac0 \U0aa4\U0aaa\U0abe\U0ab8 \U0a9b\U0ac7.";
...
But if you go ahead and use that object, you'll see that everything is actually OK:
guard
let json = originalJsonObject as? [String: Any],
let notifications = json["Notifications"] as? [[String: Any]],
let string = notifications[0]["message"] as? String else {
print("problem parsing")
return
}
print("\(string)")
That will show:
કૃપા કરીને ટોડ એપ્લિકેશન તપાસો. ત્યાં પ્રવાસ માટે એક નવી તપાસ છે.
Clearly, the details of how you extract the string will be a function of the broader JSON structure, what API you used to parse it, etc., but it illustrates the idea. Don't worry about how the NSDictionary
is displayed, but go ahead and get the String
object, and you'll see that the UTF8 string can be used just fine.
Upvotes: 4