9

Grabbing innerHTMl of this div

<div style="display:none;" id="graphdata">{n:2 , e:1},{from:1 , to:2},{from:2, to:3},{from:3, to:4}</div>

then parsing it with this JS code

jdiv=document.getElementById('graphdata').innerHTML;
edges=JSON.parse(jdiv);

JS console in Chrome says: Uncaught SyntaxError: Unexpected token n

can't find out where is that token n can be and what's wrong with my code? any ideas?

1
  • 4
    Well, that's not valid JSON. Commented Feb 17, 2013 at 22:07

2 Answers 2

20

You need to quote your labels and add brackets...

[
    {
        "n": 2,
        "e": 1
    },
    {
        "from": 1,
        "to": 2
    },
    {
        "from": 2,
        "to": 3
    },
    {
        "from": 3,
        "to": 4
    }
]
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try to take the value, split it by ',' to create an array and use JSON.stringify to generate a correct JSON. From there you can use JSON.parse and have an object

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.