0

I have to create an abacus as a json string, like this:

var jString = "'rTable':[{'1M=':'70'},{'1mv=':'70'},{'1mx=':'140'},{'1mxv=':'140'},{'1mxx=':'230'},{'1mxxv=':'230'}},{'1m+1':'90'}]";


var rTable = JSON.parse(jString);

When I try to parse it I got an unspecified "syntax error" message. Are special signs like =, + and - allowed? Can someone tell me what is wrong with this json string?

Once created I intend to get the values like this:

var score = "1M=";
var v = rTable[score];

Am I right?

1

2 Answers 2

3

A few things:

  1. Valid JSON must begin with { or [. Enclose the whole thing in {}, or remove 'rTable':.

  2. You have an extra } here: {'1mxxv=':'230'}}

  3. Use double quotes instead of single quotes.

If your language/framework is not being specific enough about syntax errors, I'd suggest running the string through JSONLint.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the prompt answer. +1 for jsonLint tip.
0

You've got a few errors.

1) JSON requires it's non-numerical keys/values to be double quoted, single quotes just won't do.

2) As another poster pointed out, JSON must start with a { or [

3) You've got an extra } at {'1mxxv=':'230'}},

Here's your valid JSON:

{"rTable":[{"1M=":"70"},{"1mv=":"70"},{"1mx=":"140"},{"1mxv=":"140"},{"1mxx=":"230"},{"1mxxv=":"230"},{"1m+1":"90"}]}

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.