简体   繁体   English

如何从json对象获取内部json

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

Suppose i have this 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;"
    }
}}    

I Have the list of those objects and puting in variable like this 我有这些对象的列表并放入这样的变量

var myobjects = data

which contains list of above objects 其中包含上述对象的列表

now that is working fine butnow i have one problem. 现在工作正常,但现在我有一个问题。 Instead of directly getting list of objects, i now have one parent object which contain those object liek this 现在我不再有直接获取对象列表的方式,而是有一个父对象,其中包含那些对象。

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

instead of getting list of childobjects , i ow have the list of parent objects in data 我没有获取子对象列表,而是在data拥有父对象列表

how can get the list of child objects from list of parent objects like before 如何像以前一样从父对象列表中获取子对象列表

something like 就像是

var childobjects = data.childobjects

I want that because i ahve templates coded which only wok if i have the direct list of objects 我想要那是因为我编码的模板只有在我有对象的直接列表时才能启动

Do you want to flatten the array of objects? 是否要展平对象阵列?

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

Then this code: 然后这段代码:

var myobjects = [];

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

Will leave myobjects equal to: myobjects等于:

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

I think it's what you need: 我认为这是您需要的:

<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