0

I have this JSON object:

"Master": {
                "nodeType": "Test",
                "label": "Master",
                "nodes": {
                    "event": {
                        "nodeType": "Test",
                        "label": "Test",
                        "itemTemplate": {
                            "label": "Test",
                            "properties": {
                                "icon": {
                                    "label": "Test",
                                    "description": "Test",
                                    "valueType": "STRING",
                                    "value": ""
                                }
                            }
                        },
                        "items": [
                            {
                                "icon": "test 2",
                            },
                            {
                                "icon": "test 1",
                            }
                        ]
                    }
                }

I want to access the items section. I've tried the following:

var obj = jQuery.parseJSON(json_object); //json object
alert(obj.nodes.items[0].icon); //Uncaught TypeError: Cannot read property 'items' of undefined 
1

4 Answers 4

1
var items;
items = obj.Master.nodes.event.items;

alert(items[0].icon);

items result in an array

note your json is incorrect as is missing a leading { and two trailing }

json_object = '{ "Master": {

//
//
//

} }';

If not correct jQuery.parseJSON will fail.

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

Comments

1

Your JSON defines a single key called "Master". Try:

alert(obj.Master.nodes.items[0].icon);

1 Comment

@free4ride got it right - items is under nodes' event key.
1
console.log(obj.Master.nodes.event.items[0].icon)

check structure of your json here : http://jsonviewer.stack.hu/

enter image description here

that will help you to understand node inheritance

Comments

-1
alert(obj.Master.nodes.event.items[0].icon);

and validate your json http://jsonlint.com/

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.