Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by roosma · May 03, 2012 at 07:16 AM · jsonescape

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mrthunderstick · May 03, 2012 at 07:47 AM

Have a read at this i had the same problem Click Here

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ComradeVanti · Oct 12, 2017 at 04:45 PM 1
Share

This link is broken. Can you repost it or explain what you found?

avatar image Chris_Entropy · Jan 12, 2018 at 02:57 PM 0
Share

I have a similar problem. Can you elaborate on the solution, since the link seems to be broken?

avatar image Bunny83 Chris_Entropy · Jan 12, 2018 at 03:36 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause Menu won't open... 1 Answer

Unity 3d xml vs json 2 Answers

Debug.Log a JSON array 2 Answers

How do I use WWWform and JSON to login to reddit? 1 Answer

Project feasibility 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges