get root json element in javascript - Stack Overflow

admin2025-04-19  1

This is my json ... using javascript .. i want to output just the name of the root in this case services then traverse through the individual elements in the array

{
    services: {
        46: {
            servicetypeid: "27",
            serviceid: "51",
            servicename: "Parking",
            description: "Parking Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        47: {
            servicetypeid: "27",
            serviceid: "52",
            servicename: "Markets",
            description: "Markets Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        48: {
            servicetypeid: "27",
            serviceid: "53",
            servicename: "PSV",
            description: "Public Service Vehicles",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        }
    }
}

This is my json ... using javascript .. i want to output just the name of the root in this case services then traverse through the individual elements in the array

{
    services: {
        46: {
            servicetypeid: "27",
            serviceid: "51",
            servicename: "Parking",
            description: "Parking Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        47: {
            servicetypeid: "27",
            serviceid: "52",
            servicename: "Markets",
            description: "Markets Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        48: {
            servicetypeid: "27",
            serviceid: "53",
            servicename: "PSV",
            description: "Public Service Vehicles",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        }
    }
}
Share Improve this question edited Aug 6, 2012 at 10:29 Esailija 140k23 gold badges279 silver badges328 bronze badges asked Aug 6, 2012 at 10:28 kmarimakmarima 752 silver badges10 bronze badges 2
  • services is not an array but an object.. you should use an array instead – Esailija Commented Aug 6, 2012 at 10:30
  • possible duplicate of How do I enumerate the properties of a javascript object? – Felix Kling Commented Aug 6, 2012 at 11:14
Add a ment  | 

1 Answer 1

Reset to default 4

That's not JSON format data, you can loop thru the object: To get the root key, you could do:

var rootKey;
for(var prop in tst) {
 console.log( prop ); //will give "services"
 rootKey = prop;
}

And to loop thru all the items:

for( var key in tst[rootKey] ) {
  for( var key1 in tst[rootKey][key] ) {
    console.log( "key:" + key1 + " --- Value:"+ tst[rootKey][key][key1] );
 }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001018a279225.html

最新回复(0)