0
\$\begingroup\$

The following is my JSON file:

{
    "data" : [
               {
                 "nameString" : "My name is \"Ansar\"",
                 "id" : 1234
               } 
    ]
}

When I try to parse this JSON file using JsonUtility, it is throwing an exception because of the \" character. If I remove the character from the string, it works smoothly.

Exception:

ArgumentException: JSON parse error: Missing a comma or '}' after an object member.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at /Users/builduser/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:50)

Why this is happening? Is this a JsonUtility bug? Anyways to fix this?

\$\endgroup\$
0
\$\begingroup\$

Found the solution.

The "nameString" : "My name is \"Ansar\"" should be kept as "nameString" : "My name is \\\"Ansar\\\"". That is Backslash is replaced with \\ .

Resource: https://www.freeformatter.com/json-escape.html

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.