我到处搜索这个但是没有得到答案。我在编码php数组后有一个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的javascript中阅读此内容
$.ajax({
type:'POST',
dataType: 'json',
data: 'var1=' + var1,
url:'myurl',
success:function(response){
alert(response);
},
error:function(e){
alert('Error:' +e);
}
});
答案 0 :(得分:0)
response
对象包含JSON。使用console.log
而不是alert
因为alert.log只会说“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]);
}
答案 1 :(得分: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];
}
},
您可以将哈希表用作单维或多维数组。