0

I have a json code and i want get json value units_num in alert jQuery. How can don it?

My json code:

[{"id":"11","name":"hiih","units_num":00}]

I tried as in js code: http://jsfiddle.net/Wj8ZL/

var obj = $.parseJSON('[{"id":"11","name":"hiih","units_num":00}]');
alert(obj['units_num']); // This don't work

var t = JSON.parse('[{"id":"11","name":"hiih","units_num":00}]');
alert(t['units_num']) // This don't work

2 Answers 2

5

Your json contains an array of objects, even if there is only one in there. So you need to access that first object in the array

var obj = $.parseJSON('[{"id":"11","name":"hiih","units_num":"00"}]');
alert(obj[0]['units_num']);
Sign up to request clarification or add additional context in comments.

1 Comment

@Taylor, To add to this answer: var obj = [{"id":"11","name":"hiih","units_num": 0}]; alert(obj[0]['units_num']); is also valid. You only need to use $.parseJSON or JSON.parse, if you are getting your JSON in the form of a string.
1

@TravisJ gave a big part of the issue, the other one being quite easy to spot if you read the error log:

"units_num":00

is not valid. It should read

"units_num":0

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.