0

假设我有这个 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

我想要那个,因为我已经编码了模板,只有当我有对象的直接列表时才可以使用

4

2 回答 2

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:''}
]
于 2013-03-27T05:45:06.250 回答
-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>
于 2013-03-27T05:58:51.060 回答