繁体   English   中英

如何从json对象获取内部json

[英]How to get the inside json from json object

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

我想要那是因为我编码的模板只有在我有对象的直接列表时才能启动

是否要展平对象阵列?

// 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:''}
]

我认为这是您需要的:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM