Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问json中的元素_Javascript_Html_Json - Fatal编程技术网

Javascript 访问json中的元素

Javascript 访问json中的元素,javascript,html,json,Javascript,Html,Json,下面是我的JSON树。如何访问给定树和框架中的Typedefinition属性,如果条件为所有Typedefinition。我必须穿过这棵树才能找到 if (typedefinition == "ciu'){//do something} else if(typedef == Tank ) else if( hostport) else if( fieldport) 并为每个if做些事情。我必须穿过这棵树,创造一个这样的条件 { "Resp_Data": {

下面是我的JSON树。如何访问给定树和框架中的
Typedefinition
属性,如果条件为所有Typedefinition。我必须穿过这棵树才能找到

if (typedefinition == "ciu'){//do something}
else if(typedef == Tank )
else if( hostport) 
else if( fieldport) 
并为每个if做些事情。我必须穿过这棵树,创造一个这样的条件

{
    "Resp_Data":
    {
        "SiteElements":
        [
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge00",
                        "ID": "1"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2003",
                        "Index": "1",
                        "Value": "00"
                    },
                    {
                        "EntityId": "2006",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2004",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    },
                    {
                        "EntityId": "9007",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "9703",
                        "Index": "1",
                        "Value": "0"
                    }
                ],
                "Connections":
                [
                    {
                        "SourceElementID": "2",
                        "DestinationElementID": "1"
                    }
                ]
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Tank1",
                        "ID": "2"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "TANK",
                        "ID": "3"
                    }
                ],
                "Entities": "",
                "Connections":
                [
                    {
                        "SourceElementID": "5",
                        "DestinationElementID": "2"
                    },
                    {
                        "SourceElementID": "2",
                        "DestinationElementID": "1"
                    }
                ]
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "CIU1",
                        "ID": "3"

                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "CIU",
                        "ID": "4"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "7050",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "9006",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "7050",
                        "Index": "2",
                        "Value": "8"
                    },
                    {
                        "EntityId": "7050",
                        "Index": "3",
                        "Value": "7"
                    },
                    {
                        "EntityId": "3037",
                        "Index": "1",
                        "Value": "1"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "\/dev\/ttyS1",
                        "ID": "4"
                    },
                    {
                        "Parent": "CIU1",
                        "ID": "3"
                    },
                    {
                        "TypeDefinition": "HOSTPORT",
                        "ID": "5"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "5022",
                        "Index": "1",
                        "Value": "9600"
                    },
                    {
                        "EntityId": "5027",
                        "Index": "1",
                        "Value": "3"
                    },
                    {
                        "EntityId": "5020",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5024",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5034",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5025",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "5019",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5006",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5012",
                        "Index": "1",
                        "Value": "3302"
                    },
                    {
                        "EntityId": "5012",
                        "Index": "2",
                        "Value": "2801"
                    },
                    {
                        "EntityId": "5013",
                        "Index": "1",
                        "Value": "10000"
                    },
                    {
                        "EntityId": "5013",
                        "Index": "2",
                        "Value": "100"
                    },
                    {
                        "EntityId": "5014",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5014",
                        "Index": "2",
                        "Value": "30000"
                    },
                    {
                        "EntityId": "5006",
                        "Index": "2",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5006",
                        "Index": "3",
                        "Value": "2"
                    },
                    {
                        "EntityId": "5005",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5007",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5009",
                        "Index": "1",
                        "Value": "3"
                    },
                    {
                        "EntityId": "5008",
                        "Index": "1",
                        "Value": "4"
                    },
                    {
                        "EntityId": "5011",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5011",
                        "Index": "2",
                        "Value": "2"
                    },
                    {
                        "EntityId": "9015",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "9016",
                        "Index": "1",
                        "Value": "1"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "\/dev\/ttyS0",
                        "ID": "5"
                    },
                    {
                        "Parent": "CIU1",
                        "ID": "3"
                    },
                    {
                        "TypeDefinition": "FIELDPORT",
                        "ID": "6"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "3504",
                        "Index": "1",
                        "Value": "40"
                    },
                    {
                        "EntityId": "3507",
                        "Index": "1",
                        "Value": "9000"
                    },
                    {
                        "EntityId": "3510",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "9011",
                        "Index": "1",
                        "Value": "7"
                    },
                    {
                        "EntityId": "9012",
                        "Index": "1",
                        "Value": "CIU1"
                    },
                    {
                        "EntityId": "3501",
                        "Index": "1",
                        "Value": "4"
                    },
                    {
                        "EntityId": "3502",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "3503",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "3505",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "3500",
                        "Index": "1",
                        "Value": "BPM-GPU"
                    },
                    {
                        "EntityId": "9013",
                        "Index": "1",
                        "Value": "3"
                    }
                ],
                "Connections":
                [
                    {
                        "SourceElementID": "5",
                        "DestinationElementID": "2"
                    }
                ]
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "CIU2",
                        "ID": "6"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "CIU",
                        "ID": "4"
                    }
                ],
                "Entities": "",
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Tank2",
                        "ID": "7"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "TANK",
                        "ID": "3"
                    }
                ],
                "Entities": "",
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Tank3",
                        "ID": "8"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "TANK",
                        "ID": "3"
                    }
                ],
                "Entities": "",
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge11",
                        "ID": "9"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2011",
                        "Index": "1",
                        "Value": "11"
                    },
                    {
                        "EntityId": "2012",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2013",
                        "Index": "1",
                        "Value": "C"
                    },
                    {
                        "EntityId": "9008",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "2016",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge22",
                        "ID": "10"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2003",
                        "Index": "1",
                        "Value": "22"
                    },
                    {
                        "EntityId": "2004",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2006",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "9007",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "9703",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge33",
                        "ID": "11"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2011",
                        "Index": "1",
                        "Value": "33"
                    },
                    {
                        "EntityId": "2012",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2013",
                        "Index": "1",
                        "Value": "C"
                    },
                    {
                        "EntityId": "9008",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "2016",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge44",
                        "ID": "12"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2011",
                        "Index": "1",
                        "Value": "44"
                    },
                    {
                        "EntityId": "2012",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2013",
                        "Index": "1",
                        "Value": "C"
                    },
                    {
                        "EntityId": "9008",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "2016",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "2003",
                        "Index": "1",
                        "Value": "44"
                    },
                    {
                        "EntityId": "2004",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2006",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "9007",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "9703",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "Gauge55",
                        "ID": "13"
                    },
                    {
                        "Parent": "",
                        "ID": ""
                    },
                    {
                        "TypeDefinition": "GAUGE",
                        "ID": "2"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "2011",
                        "Index": "1",
                        "Value": "55"
                    },
                    {
                        "EntityId": "2012",
                        "Index": "1",
                        "Value": "B"
                    },
                    {
                        "EntityId": "2013",
                        "Index": "1",
                        "Value": "C"
                    },
                    {
                        "EntityId": "9008",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "2016",
                        "Index": "1",
                        "Value": "1"
                    },
                    {
                        "EntityId": "5",
                        "Index": "1",
                        "Value": "54"
                    }
                ],
                "Connections": ""
            },
            {
                "SiteElementDef":
                [
                    {
                        "SiteElementName": "\/dev\/ttyS1",
                        "ID": "14"
                    },
                    {
                        "Parent": "CIU1",
                        "ID": "3"
                    },
                    {
                        "TypeDefinition": "FIELDPORT",
                        "ID": "6"
                    }
                ],
                "Entities":
                [
                    {
                        "EntityId": "3501",
                        "Index": "1",
                        "Value": "4"
                    },
                    {
                        "EntityId": "9011",
                        "Index": "1",
                        "Value": "7"
                    },
                    {
                        "EntityId": "3503",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "3502",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "3505",
                        "Index": "1",
                        "Value": "0"
                    },
                    {
                        "EntityId": "9012",
                        "Index": "1",
                        "Value": "CIU1"
                    },
                    {
                        "EntityId": "9013",
                        "Index": "1",
                        "Value": "2"
                    },
                    {
                        "EntityId": "3504",
                        "Index": "1",
                        "Value": "40"
                    },
                    {
                        "EntityId": "3507",
                        "Index": "1",
                        "Value": "9000"
                    },
                    {
                        "EntityId": "3510",
                        "Index": "1",
                        "Value": "2"
                    }
                ],
                "Connections": ""
            }
        ]
    }
}
试试这个:

var result=myJSONObject.Resp_Data.SiteElements[0].SiteElementDef[0].TypeDefinition 
将上述json分配给myJSONObject,然后尝试以下代码

简单

if (!obj.TypeDefinition) {
  // ..
}
else if (obj.TypeDefinition == "GAUGE") { 
  // ..
} // ...

我从结尾剪下了一些元素,使文章符合30000个字符的限制,并使用了正确的代码格式。我认为它们与这个问题无关。你可以。没有帮助吗?你喃喃自语它帮助我得到了“Microsoft JScript运行时错误:'Resp_Data.SiteElements'为null或不是对象”一个错误。现在我的要求已更改,我可以从json访问typedef元素,而无需对其进行分析“无需对其进行分析”?为什么?如果这是您得到的错误,那么您的数据结构与您发布的不一样。无论如何,这个问题太本地化了。如何访问数组和对象是JS的基本知识。看看我之前评论中的链接,或许可以阅读。您需要解决方案的速度不是我们关心的问题,我们是自愿这样做的,我们没有得到报酬。我收到了一个类似Resp_Data的错误。SiteElements[0]。SiteElementDef为null或不是Objects,那么我认为最好通过拆分来完成。试试这个,告诉我你得到数组响应数据了吗。SiteElements[0]是的,我尝试过这个…同样的错误,我得到了…你能给我推荐一个完美的代码吗..请告诉我我非常需要它..myJSONObject.Resp_Data.SiteElements[0]。SiteElementDef这也不起作用吗?我的代码类似于这个函数callme(){var Data=[{json tree}]//我现在在这里做什么来访问typedef和帧条件//函数callme(){var data=[{json tree}];//我现在在这里做什么来访问typedef和帧条件//我提到的上面的json树您的json是一个obj,所以让我们称它为
obj
然后
obj.Resp_data.sitelements[0].sitelementdef[2]TypeDefinition==“GAUGE”
true
am获取错误或“Resp_Data.sitelements”为空或不是对象