8

I did JSON.parse and getting output in javascript variable "temp" in format like this

{"2222":{"MId":106607,
"Title":"VIDEOCON Semi Automatic Marine 6.8kg",
"Name":"washma01",
}}

I tried like

alert(temp[0][0]);
alert(temp.2222[0].MId);

but not getting output.

How will I access this data in javascript ?

Sam
1,5103 gold badges19 silver badges28 bronze badges
asked Oct 15, 2009 at 5:43

4 Answers 4

17
alert(temp["2222"].MId);

You can't use numeric indexing, because don't have any actual arrays. You can use dot syntax if the first character of the key is non-numeric. E.g.:

var temp = JSON.parse('{"n2222":{"MId":106607, "Title":"VIDEOCON Semi Automatic Marine 6.8kg", "Name":"washma01", }}');
alert(temp.n2222.MId);
answered Oct 15, 2009 at 5:48
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

temp["2222"].MId

Typically temp.bar and temp["bar"] are equivalent JavaScript statements, but in this case one of your property name starts with a number. When this happens you are forced to use the index (aka bracket) notation.

answered Oct 15, 2009 at 5:52

Comments

0

You need to access the variable like so temp['2222']['MId'] , That will give you the value of MId. Even though I have shown using the [] method of getting the value , the answers below work as well.

You can run this test below in firebug.

var ss = {"2222":{"MId":106607, "Title":"VIDEOCON Semi Automatic Marine 6.8kg", "Name":"washma01"}};
console.log(ss['2222']['MId']);
answered Oct 15, 2009 at 5:50

Comments

0

when you have a good json formated object, but you don't know the key (here it look like an id) you can acces like this :

var keys = Object.keys(json_obj);
for (var i = 0; i < keys.length; i++) {
 console.log(keys[i]);
 console.log(json_obj[keys[i]].MId);
};
answered Feb 19, 2014 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.