2014-03-26 2 views
-1

나는 사방이 검색하지만 같은 멋져요에 대한 대답을하지 않았다 PHP 배열을 인코딩 후 1 JSON 객체가 오전 :읽기 JSON 객체

{ 
"6":{"brand_id":"6","brand_logo":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/account_images\/457974\/max_h-1000+max_w-1000\/tn.jpg","brand_discovered_count":"2","brand_name":"Not Human Clothing","brand_template_id":"3","products":[{"product_id":"9","product_price":"19.99","product_images":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/product_images\/131442291\/-\/blackpom2.jpg"}]}, 

"7":{"brand_id":"7","brand_logo":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/account_images\/465045\/max_h-1000+max_w-1000\/frontttt.jpeg","brand_discovered_count":"3","brand_name":"Trill LOVE","brand_template_id":"2","products":[{"product_id":"11","product_price":"49.99","product_images":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/product_images\/134025228\/-\/l3.jpeg"}]} 

} 

어떻게 내가 아약스

에서 자바 스크립트에서이를 읽을 수 있습니다
$.ajax({ 
    type:'POST', 
    dataType: 'json', 
    data: 'var1=' + var1, 
    url:'myurl', 
    success:function(response){ 

     alert(response); 
    }, 
    error:function(e){ 

    alert('Error:' +e); 
    } 
}); 
+0

[JavaScript에서 JSON을 구문 분석하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) –

답변

0

response 개체에는 JSON이 포함되어 있습니다. console.log가 방화 벽 콘솔에 객체의 전체 구조를 반복적으로 인쇄하는 동안 alert에는 "Object [Object]"만 표시되기 때문에 alert 대신 console.log을 사용하십시오.

console.log(response["6"]); // as per your example 

을 또는 당신은 모든 값을 반복하려는 경우 : 당신이 응답 오브젝트 내부 값에 액세스하려면

console.log(response) 

간단하게 할

for (i in response) { 
    console.log(i); // this should print 6, 7 and so on 
    console.log(response[i]); 
} 
0

간단한 해결책이 될 것입니다 json 데이터를 해시 테이블에 저장합니다.

success:function(response, status) { 
       for (i = 0; i <= n; i++) { 
        hashtable[response.d[i]] = [response.d[i].Col1]; 
       } 
}, 

단일 또는 다중 차원 배열로 해시 테이블을 사용할 수 있습니다

$(document).ready(function() { 
    var hashtable = {}; 

은 그런 성공의 방법에서 다음을 수행합니다.