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
<?php
header(“Content-Type: text/html; charset=utf-8”);

$text = array(
	" ",
	"Welcome",
	"my Name is...",
	"üäö",
	"blabalabl...",
	"blabal ... 

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 ... 

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 ... 

blabla",
“ß`s€”
}

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);

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 ... 

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.