-1

下面是我的 JSON 树。如果所有类型定义的条件,如何访问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": ""
            }
        ]
    }
}
4

2 回答 2

0

简单的

if (!obj.TypeDefinition) {
  // ..
}
else if (obj.TypeDefinition == "GAUGE") { 
  // ..
} // ...
于 2013-02-07T12:24:03.243 回答
0

尝试这个 :

var result=myJSONObject.Resp_Data.SiteElements[0].SiteElementDef[0].TypeDefinition 

将您的上述 json 分配给 myJSONObject 然后尝试此代码。

也参考这个

于 2013-02-07T11:57:01.003 回答