-1

Below is my JSON tree. How to access Typedefinition attribute in the given tree and frame if condition for all the Typedefinition. I have to traverse the tree find

if (typedefinition == "ciu'){//do something}
else if(typedef == Tank )
else if( hostport) 
else if( fieldport) 

and do something for each if. I have to traverse the tree and make a condition like this.

{
    "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": ""
            }
        ]
    }
}
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • I clipped a few elements from the end to make the post fit in the 30,000 character limit with the proper code formatting. I don't suppose they are relevant for the question. – JJJ Feb 07 '13 at 11:37
  • You [asked this today](http://stackoverflow.com/questions/14743790/accessing-particular-attribute-in-json). Didnt helped that? – sohel khalifa Feb 07 '13 at 11:44
  • ya mumble it dint help i got "Microsoft JScript runtime error: 'Resp_Data.SiteElements' is null or not an object" an error..now my requirement has been changed i have access the typedef element from json without parsing it – user2050531 Feb 07 '13 at 11:48
  • "Without parsing it"? Why? – JJJ Feb 07 '13 at 11:49
  • ya already its an json data....so its enough we track it as is is...without parsing....so jus having the json data help me to trach typedef and fram condition – user2050531 Feb 07 '13 at 11:51
  • so can anyone help me with the perfect coding please....i wan it as sooner as possible – user2050531 Feb 07 '13 at 11:55
  • I don't think "parsing" means what you think it means. – JJJ Feb 07 '13 at 12:12
  • No...just leave of parsing....suggest me the code to access typedef in the json tree and frame condition for it...becos am getting error like Resp_Data.SiteElements[0].SiteElementDef is null or not an object for the codes suggested by few ppl...pls do help me with perfect code...i need it very badly – user2050531 Feb 07 '13 at 12:16
  • 1
    If this is the error you are getting, then the structure of your data is not like you posted. In any case, this question is too localized. How to access arrays and objects is basic JS knowledge. Have a look at the link in my previous comment and maybe read https://developer.mozilla.org/en-US/docs/JavaScript/Guide. And how quickly you need a solution is not our concern, we are doing this voluntarily, we don't get paid. – Felix Kling Feb 07 '13 at 12:23
  • i posted the structure correctly i checked it out too – user2050531 Feb 07 '13 at 12:47

2 Answers2

0

Try this :

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

assign your above json to myJSONObject then try this code.

Refer this also

Community
  • 1
  • 1
Parvathy
  • 2,275
  • 3
  • 24
  • 39
  • am getting an error like Resp_Data.SiteElements[0].SiteElementDef is null or not an object – user2050531 Feb 07 '13 at 12:10
  • then i think its better to do by spliting. try this and tell did you got array Resp_Data.SiteElements[0] – Parvathy Feb 07 '13 at 12:17
  • ya i tried this...same error am getting – user2050531 Feb 07 '13 at 12:24
  • can u suggest me the perfect code ..pls am badly in need of it.. – user2050531 Feb 07 '13 at 12:25
  • myJSONObject.Resp_Data.SiteElements[0].SiteElementDef this is also not working? – Parvathy Feb 07 '13 at 12:26
  • http://www.json.org/js.html – Parvathy Feb 07 '13 at 12:27
  • just try the above else we can try another way. In which language u r working? java, php have good libraries – Parvathy Feb 07 '13 at 12:29
  • which code is working? – Parvathy Feb 07 '13 at 12:36
  • did you assign your json string to a variable? – Parvathy Feb 07 '13 at 12:39
  • var myJSONObject={ "Resp_Data": { "SiteElements": [ { "SiteElementDef": [ { "SiteElementName": "Gauge00", "ID": "1" }, { "Parent": "", "ID": "" },................. – Parvathy Feb 07 '13 at 12:40
  • then myJSONObject.Resp_Data.SiteElements[0].SiteElementDef[0].TypeDefinition – Parvathy Feb 07 '13 at 12:41
  • ya i did it the same way as u did – user2050531 Feb 07 '13 at 12:42
  • am getting the error 'myJSONObject' is undefined if i do as u did – user2050531 Feb 07 '13 at 12:45
  • myJSONObject.Resp_Data.SiteElements[SiteElementDef][0].TypeDefinition – Parvathy Feb 07 '13 at 12:47
  • 'Resp_Data.SiteElements' is null or not an object same error – user2050531 Feb 07 '13 at 12:48
  • Is it better to use for loop by accessing with keys – user2050531 Feb 07 '13 at 12:49
  • you use loop also http://stackoverflow.com/questions/4431641/javascript-access-nested-values-in-json-data-using-dynamic-variable-names – Parvathy Feb 07 '13 at 12:52
  • var siteElements = data["Resp_Data"]["SiteElements"]; for(var i = 0; i < siteElements.length; i++) { var siteElementDef = siteElements[i]["SiteElementDef"]; var siteElementName = ''; for(var j = 0; j < siteElementDef.length; j++) { if(siteElementDef[j]["SiteElementName"]) { siteElementName = siteElementDef[j]["SiteElementName"]; } else if(siteElementDef[j]["TypeDefinition"] == 'CIU') { alert(siteElementName); } } } is this correct this is also not working – user2050531 Feb 07 '13 at 12:54
  • are using any language? means java,php etc? – Parvathy Feb 07 '13 at 12:58
  • ya html n javascript – user2050531 Feb 07 '13 at 13:00
  • read this and try http://stackoverflow.com/questions/6158339/complex-json-and-tutorials http://stackoverflow.com/questions/10539797/complex-json-nesting-of-objects-and-arrays – Parvathy Feb 07 '13 at 13:03
  • no no...only javascript html – user2050531 Feb 07 '13 at 13:04
  • ask to anyone less i dont know why it is not working – Parvathy Feb 07 '13 at 13:08
  • callme() have json structure then where is your code to access TypeDefinition – Parvathy Feb 07 '13 at 13:09
  • @user2050531: You have to work this out on your own. People are trying to help you based on the information you provide. But then you keep repeating that it does not work because some property is not defined. You don't provide more information though. We cannot remotely debug your code, you have to do this yourself. If you are not able to, then your problem is much bigger than just accessing this data structure. We cannot teach you programming here, you have to know at least the basics. – Felix Kling Feb 07 '13 at 13:18
0

simple

if (!obj.TypeDefinition) {
  // ..
}
else if (obj.TypeDefinition == "GAUGE") { 
  // ..
} // ...
erikvold
  • 15,988
  • 11
  • 54
  • 98