- Home /
Get Json with UTF-8 encode try to convert to decoded UTF-8 String Array
hi there,
i have a Problem.. i want to get a Json with 'UnityWebRequest'.. The json is build by myself.
Here my json Code: GetARText.php
$text = array(
" ",
"Welcome",
"my Name is...",
"üäö",
"blabalabl...",
"blabal ... \n blabla",
"ß`s€"
);
foreach($text as &$arr){
$arr = utf8_encode($arr);
}
echo json_encode( $text,true);
?>
Json output:
[" ","Welcome","my Name is...","\u00fc\u00e4\u00f6","blabalabl...","blabal ... \n blabla","\u00df`s\u0080"]
c# Unity:
using (UnityWebRequest www = UnityWebRequest.Get(".../getARText.php))
{
yield return www.Send ();
if (www.isNetworkError || www.isHttpError)
{
scanText = "";
Debug.Log(www.error);
}
else
{
// Show results as text
string temp = www.downloadHandler.text;
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
byte[] bConvert = System.Text.UnicodeEncoding.Convert (System.Text.Encoding.UTF8, System.Text.Encoding.Unicode, results);
string sText = System.Text.Encoding.Unicode.GetString (bConvert);
Debug.Log (sText);
}
}
What i want:
string[] array = {" ",
"Welcome",
"my Name is...",
"üäö",
"blabalabl...",
"blabal ... \n blabla",
"ß`s€"
}
Answer by Chris-KillerSnails · Sep 05, 2019 at 07:50 PM
Had a similar issue with getting back some API requests, when I found this link: How can I convert UTF8 string to arabic?, which helped me get the correct text. I ended up using the code that @Multiaki posted:
static string DecodeEncodedNonAsciiCharacters( string value ) {
return Regex.Replace(
value,
@"\\u(?<Value>[a-fA-F0-9]{4})",
m => {
return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
} );
}
And use that method on the downloadHelper.text
like this:
string resultText = DecodeEncodedNonAsciiCharacters(www.downloadHandler.text);
Answer by Bunny83 · Sep 05, 2019 at 10:23 PM
The characters you're interested in are not UTF8 encoded but backslash encoded which is the standard for JSON. You just need to actually decode the json text. If you want to use Unity's JsonUtility you have to change your json since the JsonUtilizy does not support an array as root element. It always expects an object / associative array as root.
Since C# is a compiled strongly typed language you have to actually create a class to represent your data. Something like this:
[System.Serializable]
public class Data
{
public string[] texts;
}
In your PHP file instead of:
echo json_encode( $text,true);
you would do something like:
echo json_encode( array("texts"=>$text),true);
Which should produce
{ "texts" : [" ","Welcome","my Name is...","\u00fc\u00e4\u00f6","blabalabl...","blabal ... \n blabla","\u00df`s\u0080"] }
This can be decoded in Unity like this:
Data data = JsonUtility.FromJson<Data>(yourJsonText);
Note all I wrote here is untested. If you find any errors, please tell me.
Your answer
Follow this Question
Related Questions
C# Convert json arrays to unity arrys 3 Answers
Get JSON array object string value 2 Answers
Help using LitJson 1 Answer
C# Converting json arrays using JsonUtility, 1 Answer
Multiple Cars not working 1 Answer