使用Jquery解析Json对象中的问题

时间:2013-11-22 13:30:09

标签: javascript jquery json parsing

我有以下JSON对象。

{
    "newArrival": {
        "head": {
            "name": "New Arrivals",
            "url": "/new-arrivals/"
        },
        "banner": [
            {
                "name": "Lee Cooper",
                "url": "/Lee_Cooper/",
                "src": "http://indfas1.images.com/images/abc/menu/new-arrival-lee-cooper-1.jpg",
                "alt": "",
                "title": ""
            },
            {
                "name": "Ray Ban",
                "url": "/Ray-Ban/",
                "src": "http://indfas1.images.com/images/abc/menu/new-arrival-ray-ban-1.jpg",
                "alt": "",
                "title": ""
            },
            {
                "name": "Vero Moda",
                "url": "/vero-moda/",
                "src": "http://indfas1.images.com/images/abc/menu/new-arrival-vero-moda-1.jpg",
                "alt": "",
                "title": ""
            }
        ]
    }
}

我想读取head和banner标签的值。我正在使用以下Jquery代码:

$.each(json.newArrival, function (i, newArr) {
    $.each(newArr, function (index, value) {
        alert(value);
    }); 
});

但是,我无法以任何方式阅读头部和横幅。

任何人请帮助!!

提前多多感谢。

- 阿苏

4 个答案:

答案 0 :(得分:1)

您拥有的代码“按原样”运行。但是,您必须访问头部数据和横幅数据之间存在差异。

因为head是一个对象,你的内部将遍历其成员,即newArrival.head.name和newArrivale.head.url。

因为banner是一个数组,所以你将迭代对象。如果你然后通过这些对象,你将访问name,url,src等。

因此,您所拥有的应按顺序提醒新来者,/新到来/,[对象对象],[对象对象],[对象对象]

为什么不直接访问newArrival.head的成员(如果需要),然后使用

遍历newArrival.banner
alert(json.newArrival.head.name);
alert(json.newArrival.head.url);
$.each(json.newArrival.banner, function (i, newArr) {
    $.each(newArr, function (index, value) {
        alert(value);
    });
});

答案 1 :(得分:0)

转到你想要的节点代替json.newArrival.banner

$.each(json.newArrival.banner, function (i, newArr) {
    alert(newArr.name);
});

fiddle

答案 2 :(得分:0)

试试这个

var data = JSON.stringify({
"newArrival": {
    "head": {
        "name": "New Arrivals",
        "url": "/new-arrivals/"
    },
    "banner": [
        {
            "name": "Lee Cooper",
            "url": "/Lee_Cooper/",
            "src": "http://indfas1.images.com/images/abc/menu/new-arrival-lee-cooper-1.jpg",
            "alt": "",
            "title": ""
        },
        {
            "name": "Ray Ban",
            "url": "/Ray-Ban/",
            "src": "http://indfas1.images.com/images/abc/menu/new-arrival-ray-ban-1.jpg",
            "alt": "",
            "title": ""
        },
        {
            "name": "Vero Moda",
            "url": "/vero-moda/",
            "src": "http://indfas1.images.com/images/abc/menu/new-arrival-vero-moda-1.jpg",
            "alt": "",
            "title": ""
        }
    ]
}
})


 var data1 = JSON.parse(data) 

 console.log(data1.newArrival.head.name)
 console.log(data1.newArrival.head.url)

 console.log(data1.newArrival.banner[0].name)
 console.log(data1.newArrival.banner[0].url)
 console.log(data1.newArrival.banner[0].src)
 console.log(data1.newArrival.banner[0].src)

这会对你有所帮助

答案 3 :(得分:0)

试试这个..

var JsonObj = Say yourValues
var headObj = JsonObj.newArrival.head; //head json values 
var bannerObj = JsonObj.newArrival.banner; //banner json Values

假设您要访问 bannerObj 第一个名字......

 alert(bannerObj [0].name);