I am posting JSON requests to an URL using Unity and C#. I am getting back responses in JSON format with texts containing some special characters. Such as: "ó", "ü","é" etc.
For the posting and receiving, I am using the WWW class of Unity. I have tried to convert the incoming byte[] the following way:
byte[] incomingBytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, www.downloadHandler.data);
var str = Encoding.Unicode.GetString(incomingBytes);
Debug.Log(str);
It makes no effect on the output. I am still getting the following example text back: csap\u00e1sra instead of csapásra. Any idea how to get back a field containing special characters from a JSON in Unity/C#?
Related
I'm returning objects as JSON results from my APIController inside an ASP.NET MVC web-API.
To convert objects to JSON results I use the below syntax:
return Ok(new WebServiceResult(...));
Are there any methods to convert strings automatically to BASE64 encoding during object to JSON conversions?
Can OK handle non-English characters during JSON conversions and avoid failure to decode that string on other platforms (I mean does it support encodings like UTF-8 or does it handle escape characters that reside inside the contents which are going to be converted such as { character or : character)?
I'm using Base64 encoding to store values from my data structure into a string.
Basically what I do is convert a byte array into base64 string
string StoredData = Convert.ToBase64String(ByteArray);
I then divide StoredData into strings of a maximum length of 256 Characters and store them as an ASCII string (in AutoCAD XData as an DxfCode.ExtendedDataAsciiString) .
When I want to retrieve my data I do the following:
First I combine each 256 long string using StoredData = sting1 + string2 + ...
Then I convert StoredData back into ByteArray using
var ByteArray = Convert.FromBase64String(StoredData);
Now this has worked great for me and my clients until a month ago, where one of my clients has had some crash and errors popping up.
I asked him to send me his stored data, and I got surprised to see that his data contained invalid Base64 Characters (see sample below)
tM7x24QLLLALr5ivAx3XFAM7uciYXrCjKXSFd3XOL/KGIc3C+JMO8QjHT/4c+puYrNLq5r9Is0vpDKyuxw9I6R3f1LuOYSdHS6XgZJEyMvGwSHNRSYJ/a0IoumQftB3XspQRwp4QSd7qcUVsrXw0+2RS/sd2vAvUFxEQgwsHaabb01YjchGeyxr1f78A4qy2BL/oHAsRak9UYN0mDzhZgbhpahlgdK3eWd8b2BTM01lWh74pYUrJR+JfQ0tw0Eu㿔
Z/1JxBMUv2cB6NrFehSuNF9l4dhAaZQ+TcIClZmk/ZC8TJ0rKka/J+HqhLDAwWExB3nXoIi00uJnE7J4R6rU+Q==
as you can see the first 256 long string had an invalid Base64 character (㿔)
Why is that happening? can this be related to the users computer? I tried to replicate this error without any success and because I don't have access to their computers, I'm starting to think it might be something on their side.
The application uses .Net framework version 4.5.
Edit: it turned out client has sent me a recovered document which didn't recover the text strings properly which explains the corrupted string.
It turns out the app has crashed and client has recovered the drawing document with corrupted string.
I receive form API a JSON message which looks like:
{"message":"Nieprawid\u0142owy format"}
"\u0142" is polish charakter "ł". How can I convert the whole JSON message, as it may contain much more signs encoded this way? I have tried HTMLDecode, URLDecode and few others but none worked.
I try do to this with c# and asp.net. Thanks for any ideas.
A JSON parser will take care of the conversion:
var json = (JObject)JsonConvert.DeserializeObject(str);
I have a c# program that retrieve some JSON data and use Newtonsoft JSON to Deserialize it.
as i use persian chars in my program the JSON codes will be shown like this:\u060c \u067e\u0644\u0627\u06a9 .... also after i retrive the JSON data in my program this chars still show like its coded sample.but after i Deserialize it converted to ???? chars.
what should i do?
Your JSON deserializer is broken; \uXXXX is supposed to be turned into proper characters.
To do that yourself, use this function
// Turns every occurrence of \uXXXX into a proper character
void UnencodeJSONUnicode(string str) {
return Regex.Replace(str,
#"\\u(?<value>[0-9a-f]{4})",
match => {
string digits = match.Groups["value"].Value;
int number = int.Parse(digits, NumberStyles.HexNumber);
return char.ConvertFromUtf32(number);
});
}
(Untested code; I don't have VS available at the moment. Some exception handling would probably be nice too)
Looks like it has been JSON encoded, so you need to decode it. The DataContractJsonSerializer class can do this.
See this MSDN link for more information.
I am Encrypted using AES and passing in querystring, will Html.Encode convert all the characters properly such that calling Decode will result in the same string?
I was trying to do the same thing, and found the answer here:
Encrypting a value using MySQL's AES_ECRYPT function, then passing it in a URL string, using PHP
The slashes can cause problems, even when url encoded. The solution that is working for me is to convert to hex, pass it through the URL, then revert to binary before decrypting.
HTML encoding is different from URL encoding. HTML encoding is used when you want to output a URL in an HTML document. It escapes HTML stuff. To output a URL in an HTML page you should first URL encode the values to generate a valid URL and then HTML encode it when you want to write it in an HTML page.
Use HttpUtility.UrlEncode. Alternatively, you could first convert the byte[] to base64 using Convert.ToBase64String and then encode it using HttpUtility.UrlEncode. It's likely to generate a shorter URL.
Calling HttpUtility.UrlEncode before putting it in the query string will encode it correctly.
On the receiving side, the QueryString property already decodes the values, so you shouldnt call any decoding methods (other than Convert.FromBase64String)
AES encrypts in a byte oriented fashion. To transmit bytes in the query string you'll need to convert it to text. One way to do that is to use Convert.ToBase64String().
Once it has been converted to text you'll need to make sure any non-alphanumerics are encoded properly via UrlEncode().
On the receiving end if it's already UrlDecoded() you should be able to convert the text into an encrypted byte stream via Convert.FromBase64String() then decrypt the resulting byte array.