2014-03-26 213 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]; 
       } 
}, 

可以使用散列表作为单个或多维数组。