0

I am getting the variable from a json file that is

var node = data.nodes;

alert(node); returns the following

[{"name" : "30","group": 0} , {"name" : "40","group": 0} ]

which is not an Object

If I assign this value directly to a variable then it is counted as object as you can see below.

var node = [{"name" : "30","group": 0} , {"name" : "40","group": 0} ]

Why is the value not an Object in the first place? What can I do to convert the variable to an Object?

any help would be truly appreciated.

1
  • Yes it is :) @Petrichor Commented Aug 11, 2015 at 7:32

2 Answers 2

4

You can use the JSON.parse method to turn the string into an object:

var node = JSON.parse(data.nodes);

Note that some older browsers (e.g. IE 7) doesn't support the JSON object. You can read more about that on the documentation page that I linked to if you need to support older versions.

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

Comments

3

Try this:

<script type="text/javascript">
var node = data.nodes //[{"name" : "30","group": 0} , {"name" : "40","group": 0} ]
var data = JSON.parse(node);
console.log(data); //{name: "30", group: 0}, {name: "40", group: 0}
console.log(data[0]); //{name: "30", group: 0}
</script>

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.