如何从json对象获取内部json

时间:2013-03-27 05:40:43

标签: javascript json

假设我有这个json

{"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;"
    }
}}    

我有这些对象的列表并放入像这样的变量

var myobjects = data

包含上述对象列表

现在工作正常但是我有一个问题。我现在有一个包含这个对象的父对象,而不是直接获取对象列表

"id": "21"
"myobject": 
{
{"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;"
    }
}}
}   

而不是获取子对象列表,我有data

中的父对象列表

如何从之前的父对象列表中获取子对象列表

类似

var childobjects = data.childobjects

我想要那个,因为我编写的模板只有在我拥有对象的直接列表时才能进行编码

2 个答案:

答案 0 :(得分:0)

你想展平对象数组吗?

// suppose data is like this:
var data = [
    { id: 1, myobject: {widgets:'',whatever:''}},
    { id: 5, myobject: {widgets:'',whatever:''}},
    { id: 23, myobject: {widgets:'',whatever:''}}
]

然后这段代码:

var myobjects = [];

for (key in data) {
  myobjects.push(data[key].myobject);
}

myobjects等于:

[
    {widgets:'',whatever:''},
    {widgets:'',whatever:''},
    {widgets:'',whatever:''}
]

答案 1 :(得分:-1)

我认为这就是你需要的:

<script type="text/javascript">
var parents = [{"id": "21",
                "myobject": 
                 {"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;"
                 }
             }
         }},
                {"id": "22",
                "myobject": 
                 {"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;"
                 }
             }
         }}
    ]; 
   for(x in parents ){
        console.log(parents[x].myobject);
   }
   </script>