0

I have one json object constructed as follows:

<?
$t['w'][$id]['marchin']=$machin;
$t['w'][$id]['c'][$id_com]['machin']=$machin;    
echo json_encode($t);
?>

I browse the object like this

// here i access to all the $t['w'][$id]

$.each(data.w, function(k,v){

              var that=this;
              doSomething();

              // now i want to access to all the $t['w'][$id]['c']
               $.each(that.c, function(k1,v1){
                     doSomething();       
               });

});

but here at the second each jquery make an error .. How to access all $ t ['w'] [$ id] ['c']?!

thank you


Ok i tried :

              $.each(data.w, function(k,v){
                  var that = $.parseJSON(this);
                        doSomething();

                    $.each(that[k]['c'], function(k1,v1){
                        doSomething();

                 });

       });

but it does not work again,

here's an example of my json,

{"w":
   {"3":
      {"test":"test","c":
        {"15":
           {"test2":"test2"}
        }
      }
    }
}
2
  • Would you mind post the json please ? Commented Sep 16, 2012 at 21:22
  • Did you start by parsing the json string back to an object with var myObj = $.parseJSON(data); Commented Sep 16, 2012 at 21:23

2 Answers 2

1

Data ...

var data = {"w":
   {"3": {
       "test":"test",
       "c": {
          "15": {"test2":"test2"}
       }
      }
    }
};

Loop ...

$.each(data.w, function(key, value){
    // you are now lopping over 2nd dimension
    // On each loop, 'key' will be equal to another [$id] value
    // since you know you'd like to crawl 'c' sub entry, you can reference it
    // and loop over it
    $('body').append('<h3>'+key+'</h3>');
    if( this['c'] )
    $.each(this['c'], function(k,v){
        // Here you have access to ['c'][...]
        $('body').append('<span>'+k+'</span>');
    });
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

sure, let's fiddle this ... jsfiddle.net/ZDb6j/ ... works now ... going to edit my post
0

You can do this without .each at all:

var hsh = $.parseJSON(data.w);

for (var i in hsh) {
  var that = hsh[i];
  doSomething();

  for (var j in hsh[i].c) {
    doSomething();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.