0

I want to read JSON objects's name value pair. Sample JSON below:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "**menuitem**": [
      {"**value**": "New", "**onclick**": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}

As an example, if I want to read name "menuitem" and its values "value", "onclick". How is "menuitem" referred in this context?

Wes Crow
  • 2,971
  • 20
  • 24
asd
  • 101
  • 1
  • 1
  • 8

1 Answers1

1

menu.popup.menuitem[0].value

menu.popup.menuitem[0].onclick

(you could also run through a loop if you wanted to get something rather than the first menuitem)

I assume there was asterisks in your code because you were trying to bold the sections in the json that you wanted to retrieve?

Also that json isn't valid, you're missing a closing }

aprea
  • 109
  • 1
  • 2
  • 12
  • yes.. those asterisks were to bold them.. btw, the answer u have provided is to retrieve menuitem's values (value, onclick).. but how can i retrieve menuitem itself..? – asd Jun 01 '11 at 04:43
  • `menu.popup.menuitem[0]` it will return an array – aprea Jun 01 '11 at 04:45
  • how can i identify that there is a name "menuitem" under popup? how can i retrieve "menuitem" which is under popup? – asd Jun 01 '11 at 04:49
  • if you want to check if key actually exists, see this post: http://stackoverflow.com/questions/1098040/checking-if-an-associative-array-key-exists-in-javascript – aprea Jun 01 '11 at 05:07