1

I am new to JavaScript. I have an object like the below. Now I want to get the object "window" only. How can I do it?

var jsonData = {

    "widget": {
        "debug": "on",
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        },
        "image": { 
            "src": "Images/Sun.png",
            "name": "sun1",
            "hOffset": 250,
            "vOffset": 250,
            "alignment": "center"
        },
        "text": {
            "data": "Click Here",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
    }
};
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50

2 Answers2

2

You can do:

var windowObj = jsonData.widget.window; //where data is your object
tymeJV
  • 103,943
  • 14
  • 161
  • 157
1
var windowobj = jsonData.widget.window;

Note that if you are actually declaring your object literally like that, there is no JSON anywhere, so naming the variable jsonData is a bit strange.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • why not json? the object pasted into a json parser parses just fine. if it's not json, why would the structure parse as JSON? – dandavis Sep 18 '13 at 20:20
  • @dandavis Because JSON is a subset of Javascript object literal notation, which is what the OP is using (if it were all in a string, it would be JSON, which is why I said "if you are actually declaring your object literally like that"). If it comes from an AJAX request for xample, then it is JSON. – Paul Sep 18 '13 at 21:29