0

I am creating an JSON file which stores some Physics equation, which will be rendered using MathJax.

"equations": [
    "$w = F.s\cos\theta$"
 ]

I am getting a bad string error. I have tried adding another backslash before the slashes but that changes the equations drastically. Is there any way to fix this issue without changing the equation

2 Answers 2

4

There were two issues you were falling over.

Firstly, a valid JSON file will have { and } around it (as David Gatti mentions in his answer, it is an object after all). Secondly, certain characters - including backslashes - will need to be escaped. When you parse it back into an object, the additional backslashes will be removed.

Your corrected JSON should read:

{
    "equations": [
        "$w = F.s\\cos\\theta$ "
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

JSON is an encoding of structured data. You write

{
  "equations": [
    "$w = F.s\\cos\\theta$"
  ]
}

to mean an object with a property named equations with an array with a single string:

$w = F.s\cos\theta$

The escaped backslashes (\) does not change the underlying data. They get removed by the receiver when the JSON gets decoded into the object graph.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.