0

I have the following json output, I am unable to by pass when it comes to these characters.

Json String

{
    "results": {
        "RESULT1-Node1": {
            "Network.MS": "405",
            "Down_time": "131"

        },
        "RESULT4-Node2": {           
            "Network.MS": "451",
            "Down_time": "141"                         }
             }
}

Javascript

     for (var resultBank in jsonData.results) {
            var rootType = resultBank ;
            console.log(rootType );
             for(var result in eval("resultBank."+JSON.stringify(rootType)) ){

                console.log(result[result]); 

             }  
}
Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44
  • What data do you intend to get? – Joseph Jun 04 '13 at 09:57
  • `rootType` is the same as `resultBank`. Why would you are trying to access `resultBank.rootType`? I think you want `jsonData.results[resultBank]`. – Felix Kling Jun 04 '13 at 09:58
  • 1
    possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) and [how to access any element using javascript, if elemet is having special characters](http://stackoverflow.com/q/12953704/218196). – Felix Kling Jun 04 '13 at 09:59
  • rootType to be "RESULT1-Node1" – Njax3SmmM2x2a0Zf7Hpd Jun 04 '13 at 10:01
  • 1
    You don't seem to know how `for...in` works, so have a look at the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in). This might help as well: [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/q/85992/218196) – Felix Kling Jun 04 '13 at 10:02
  • @Macon: Exactly. So you are trying to access `"RESULT1-Node1"."RESULT1-Node1"` which doesn't make sense. Strings don't have a `"RESULT1-Node1"` property (even if it was valid syntax). – Felix Kling Jun 04 '13 at 10:03

1 Answers1

1

When using for (var x in y) to loop through y, the x variable is set to the index of each item. Therefore, to get the item itself, you use y[x].

for (var resultBank in jsonData.results) {
    var rootType = resultBank ;
    console.log(rootType );
    for(var result in jsonData.results[resultBank]) {

        console.log(jsonData.results[resultBank][result]); 

    }  
}
Pudge601
  • 2,048
  • 1
  • 12
  • 11