user3553401
user3553401

Reputation: 109

json string not parsing

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?

Upvotes: 0

Views: 61

Answers (2)

Adam Jenkins
Adam Jenkins

Reputation: 55623

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"}]}

Upvotes: 0

Todd Menier
Todd Menier

Reputation: 39309

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.

Upvotes: 3

Related Questions