- Home /
Valid JSON string not working in Unity
When I'm sending in a JSON string from javascript to Unity I get the following warning in the debugger console on the page: Uncaught SyntaxError: Unexpected token ILLEGAL
The JSON I'm sending looks like this: {"purchase_info":"You can purchase the product by clicking the \"Purchase\"-button below."}
I tried debugging this in Unity and put the JSON as a string in a UnityScript file and immediately got the following error: unexpected char: '"'
It would seem that UnityScript doesn't like strings that are correctly JSON encoded, but it seems ok if I double escape the double quote. Any comments on this? It seems a little silly to have to reformat the string into an invalid JSON before sending it to Unity.
Answer by mrthunderstick · May 03, 2012 at 07:47 AM
Have a read at this i had the same problem Click Here
This link is broken. Can you repost it or explain what you found?
I have a similar problem. Can you elaborate on the solution, since the link seems to be broken?
You most likely do not have the same problem. This problem seems to be about a wrong formatted string literal in a script file. This has actually nothing to do with the JSON format but how you have to escape certain characters in string literals in the program$$anonymous$$g language of your choice.
The correctly written string literal in UnityScript or C# would be:
var someStr = "{\"purchase_info\":\"You can purchase the product by clicking the \\\"Purchase\\\"-button below.\"}";
That is because a string literal is delimited with quotes in a script file. So obviously we can't just write quote character inside a string. So the quote character needs to be escaped. The "someStr" variable will contain the string
{"purchase_info":"You can purchase the product by clicking the \"Purchase\"-button below."}
at runtime. The backslash is the escape character in UnityScript and C# string literals. So if you want an actuall backslash to appear in the string you have to escape it "\\". However as mentioned earlier when you want a quote character inside a string literal it has to be escaped as well "\"". If you want a backslash and a quote inside the string it looks like this: "\\\""
However in most cases you don't include json text directly in a script file. If you have an external json file it would just contain the normal json text.
If you have any actual question, please ask your own seperate question. Also back then when this question was asked Unity didn't have any built-in functions to parse json text. The wiki link in this answer just points to the JSONObject article but uses the outdated domain. I'll fix the link. Though this is just another JSON parser. It won't solve any issues you have with wrong string literal in script files and it won't solve any mis-formatted json text.
Answer by Kamil1064 · Aug 21, 2020 at 07:53 AM
I had problem which i googled and came here so I will post my answer here, maybe someone gonna use it. I'm using localization system from unity's tutorial. To read it I'm using code:
LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
for (int i = 0; i < loadedData.items.Length; i++)
{
localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
}
Yesterday I was working on rest of my text files and writing them into another language. Properly line looks like that:
{"key":"Tutorial_012","value":"So here you may upgrade basic stats.\nYou may see stats of your car before buy,\njust use buttons to see how much will cost desired result.\n\nLet's increase TOP SPEED"},
To set new line of text as you may see I'm using \n. Problem which took me whole day to solve was just one mistake :)
{"key":"Tutorial_012","value":"So here you may upgrade basic stats.\nYou may see stats of your car before buy,n\just use buttons to see how much will cost desired result.\n\nLet's increase TOP SPEED"},
On third line of my text I used 'n\' where it must be '\n' So just be carefull :)
Cheers,
Kamil