-2
{
    "name": "Top Bar After Login",
    "cid": "9921",
    "position": "26",
    "properties": [{
        "propname": "ITEMCOUNT",
        "propvalue": "0"
    }, {
        "propname": "Display",
        "propvalue": "yes"
    }],
    "childs": [{
        "name": "Santosh",
        "cid": "10299",
        "properties": [{
            "propname": "Display",
            "propvalue": "yes"
        }, {
            "propname": "ITEMCOUNT",
            "propvalue": "0"
        }],
        "childs": []
    }]
}

How to read the child's property in the above JSON object?

I have tried this example Parse JSON in JavaScript? but it didn't help me out to fetch child's properties.

Thanks for the help in advance.

Thanks, Santosh

Community
  • 1
  • 1
Santosh
  • 5
  • 2
  • 7
  • First of all you need to understand a difference between JSON and javascript object. You deal with plain javascript object. It's not JSON. – dfsq Mar 03 '14 at 07:30
  • `var parsed_obj = { ... }; parsed_obj.properties[0]; parsed_obj.properties[1];` ? – Matteo Tassinari Mar 03 '14 at 07:31
  • Hi, I have 4 levels of sub categories. The above one I posted just for an example. Could you please tell me how I can fetch all the levels. Means data ={[childs={[childs={[childs{[childs={[]}]}]}]}]} I am able to read first level of child properties – Santosh Mar 03 '14 at 13:01
  • I am using this code to get the first level of child properties ---------------------------------------------------------------------------------- var count = Object.keys(data).length for(var i=0; i – Santosh Mar 03 '14 at 13:03
  • https://github.com/janl/mustache.js is the solution for my above problem. – Santosh Mar 07 '14 at 08:24
  • @ dfsq - it is JSON data which I am getting by hitting an URL. – Santosh Jun 24 '14 at 10:15

2 Answers2

1

If you store your object above in a variable, say parsed_obj then you simply do

parsed_obj.properties[0];

to access its first property and

parsed_obj.properties[1];

for the second one.

Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
  • Hi, I have 4 levels of sub categories. The above one I posted just for an example. Could you please tell me how I can fetch all the levels. Means data ={[childs={[childs={[childs{[childs={[]}]}]}]}]} – Santosh Mar 03 '14 at 12:58
  • I am not sure what you mean by "fetch all the level", but you can use dot notation to go as deep as you want, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators – Matteo Tassinari Mar 03 '14 at 13:29
1

Did you do it like this?

var source = '{"name": "Top Bar After Login", "cid": "9921", "position": "26", "properties": [{"propname": "ITEMCOUNT","propvalue": "0"},{"propname": "Display", "propvalue": "yes"}],    "childs": [{"name": "Santosh","cid": "10299","properties": [{"propname": "Display", "propvalue": "yes" }, { "propname": "ITEMCOUNT", "propvalue": "0"}], "childs": [] }]}';

var obj = JSON.parse(source);
console.log( obj.childs[0].name ); //outputs Santosh
console.log( obj.name );            //Top Bar After Login 
console.log( obj.properties[0].propname,  obj.properties[0].propvalue ); //ITEMCOUNT 0

Hope it helps you

  • Hi, I have 4 levels of sub categories. The above one I posted just for an example. Could you please tell me how I can fetch all the levels. Means data ={[childs={[childs={[childs{[childs={[]}]}]}]}]} – Santosh Mar 03 '14 at 12:55
  • obj.childs.childs.childs.childs[0].properties[0].propname or obj.childs[0].childs[0].childs[0].childs[0].properties[0].propname But your sample code says obj.childs.childs – Nils Thiebosch Mar 04 '14 at 08:14
  • Hi Holger Thiebosch, Thanks for your help. Could you please help me in getting multiple levels of child properties. – Santosh Mar 05 '14 at 05:20