-1

I have to create json dynamically, which is not a problem but in one place json name and value pair are random and can also be different, so is it possible to read json.

var json = {
        "set":[
{
    "name":"Device 1",
    "children":[ 
    {
        "name":"name",
       "name1":"name",
       "xyz":"hello",
       "abc":"hello1",
       "vwx":"hello2",
       "stu":"hello3",      
        "children":[
        {
            "type1":"iname",
            "type2":"cname",
            "type3":"pname"
        }
        ]
    }
    ]
},{
    "name":"Device 2",
    "children":[ 
    {
        "name":"name3",
       "name4":"name4",
       "def":"hello2",
       "lmn":"hello3",      
        "children":[
        {
            "type1":"iname",
            "type2":"cname",
            "type3":"pname"
        }
        ]
    }
    ]
}
]};

This is a part I have done to iterate data before children.But as my doubt was only for data between children that's why I have shorten the json to only limit it to the question I have asked

$(window).bind("load", function() {
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
   mouseX = e.pageX; 
   mouseY = e.pageY;
});
    $.each(device_json.device, function(i, v) {
      $('#front'+v.position).append(v.name);
            $('#front'+v.position).hover(function(){
                $('#front'+v.position).append('<div class="hover"><b>Name :</b> ' +v.name+'</div>').css({'cursor':'pointer'}).show();
                $('.hover').css({'top':mouseY,'left':mouseX});
                },function(){
                    $('.hover').remove();
                });

I have to show the data between children in table form first td will be name and second will show value, but how many are there in between children is unknown and also there sequence is not same.Any help will be appreciated.Thanks in advance

Ratish
  • 65
  • 3
  • 12
  • Would be a lot easier if you standardize structures. Not sure how you would want this in table either. Show expected results – charlietfl Mar 07 '16 at 16:06
  • Possible duplicate of [How do I iterate over a JSON structure?](http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure) –  Mar 07 '16 at 16:07
  • I am using a standardize structure but the data between children have no link except they r part of their parent. and I have to display in table only the data in between children. I am able to handle other data but confused to use them for data between children. – Ratish Mar 07 '16 at 17:20

1 Answers1

0

"children" is an array of objects, you could loop on these objects properties. Here is a way of doing that:

for (var i = 0, keys = Object.keys(object), l = keys.length; i < l; ++i){
    propertyValue = object[keys[i]];
}

You can find others at https://jsperf.com/iterating-over-object-properties/2

Then you could test the name of the property, to see if it is a "children" array, or something else.

If you have access to what is creating this json, maybe you can also change its organization to avoid your issue.

Cithel
  • 686
  • 7
  • 21