2012-09-16 2 views
0

를 사용하여 JSON 개체 찾아보기 : 나는 각 JQuery와 확인 오류이나는 다음과 같이 하나의 JSON 개체를 구축해야 JQuery와

// 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();  
       }); 

}); 

처럼하지만, 여기에 두 번째에서 객체를 검색

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

을 .. $ t [ 'w'] [$ id] [ 'c'] 모두 액세스하는 방법 ?!

당신을 감사


좋아, 내가 시도 : 여기

내 JSON의 예

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

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

       }); 

     }); 

하지만 다시 작동하지 않습니다,

{"w": 
    {"3": 
     {"test":"test","c": 
     {"15": 
      {"test2":"test2"} 
     } 
     } 
    } 
} 
+0

당신은 JSON을 게시 마음을 주시겠습니까? – Stphane

+0

json 문자열을 'var myObj = $ .parseJSON (data);'로 다시 구문 분석 했습니까? – adeneo

답변

1

데이터 ...

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

루프 ...

$.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>'); 
    }); 
}); 
+0

이 코드는 작동하지 않습니다. 도움을받을 수 있습니까? – user1475195

+0

확실히, 이걸 피델 ... jsfiddle.net/ZDb6j/ ... 지금 작동 ... 내 게시물을 편집하려고합니다. – Stphane

0

당신은 모든 .each없이이 작업을 수행 할 수 있습니다

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

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

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