2014-03-26 147 views
-1

我尋找這個到處都是,但沒有得到答案this.I編碼我PHP數組後有一個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"}]} 

} 

我怎麼能在阿賈克斯

讀這在javascript
$.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,因爲alert只會說「Object [Object]」,而console.log會遞歸地將對象的整個結構打印到Firebug控制檯中。

console.log(response) 

如果您要訪問的響應對象內的值簡單地做:

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

或者,如果你想通過所有值進行迭代:

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

一個簡單的解決方案將是將json數據保存在散列表中。

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

然後在成功方法執行以下操作:

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

可以使用散列表作爲單個或多維數組。