0

I have a problem loop through a json file. i need to do special thing for each of root items (i mean c0,c1,c2,....) and another actions for each of their nested keys (exactly for each "series" that a root key have).

for example do an action for c0 and do some actions for series.s0 and series.s1 of c0 amd conitune....

here is my json file:

{
    "c0": {
        "name": "statistics",
        "series": {
            "s0": {
                "name":"Inbound",
                ....
            },
            "s1": {
                "name":"Outbound",
                ...

            }   
            }
        },
    "c1": {
        "name": "Packet statistics",
        "series": {
            "s0": {
                "name":"pjS",
                ...
            },
            "s1": {
                "name":"-----",
                ...
            }   
            }
        },
    "c2": {
        "name": "Bstatistics",
        "series": {
            "s0": {
                "name":"Active",
                ....
            },
            "s1": {
                "name":"SPI",
                ....

            }   
            }
        },
    "c3": {
        "name": "DPacket",
        "series": {
            "s0": {
                "name":"policy",
                ...
            }   
            }
        }

        }
user1229351
  • 1,985
  • 4
  • 20
  • 24
  • 1
    these are not root elements, regular ones. I have a feeling you didnt try anything yet, how about a `for` or `google` ? provide some sample code that you tried or it looks like you are outsourcing your work to stackoverflow -.- – n00b May 05 '13 at 15:36
  • And what did you try to loop over it? What is your *actual* problem? – zmo May 05 '13 at 15:36
  • If you control the structure of the JSON, I'd suggest using arrays instead of objects with incrementing keys like `"c0", "c1", "c2", etc` –  May 05 '13 at 15:38
  • @n00b its not outsourcing! i have never used json object before, then i don't know how to loop through them. in other words i don't have any idea how to do that. – user1229351 May 05 '13 at 15:42
  • After you decode the JSON, it's just an ordinary Javascript object, that you process just like if you'd created it directly in JS with `var obj = { foo: bar };` syntax. There's no such thing as a "json object". – Barmar May 05 '13 at 16:09

1 Answers1

2

Try something like:

var a = { 'c1': {...}, 'c2': {..}, 'c3': {...} };

console.log(a['c1']);   // or alert(a['c1'])

for (i in a) {
    console.log(i);
    for (j in a[i]) {
        console.log(j);
    }
}
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44