打印阵列?

时间:2015-02-11 17:59:43

标签: javascript jquery json

请帮助引导json-array的标题 我的尝试:

(function (jQuery){
    var json = {
        "news": [{
            "img": "http://static3.www.net/img/300x300/2257740.jpeg",
            "title": "qwerty1",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674134&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674134"
        }, {
            "img": "http://static5.www.net/img/300x300/2257778.jpeg",
            "title": "qwerty2",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674190&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674190"
        }, {
            "img": "http://static3.www.net/img/300x300/2257776.jpeg",
            "title": "qwerty3",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674184&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674184"
        }, {
            "img": "http://static2.www.net/img/300x300/2257748.jpeg",
            "title": "qwerty4",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674145&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674145"
        }, {
            "img": "http://static1.www.net/img/300x300/2257766.jpeg",
            "title": "qwerty5",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674174&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674174"
        }, {
            "img": "http://static3.www.net/img/300x300/2257750.jpeg",
            "title": "qwerty6",
            "url": "http://news.net.www.ru/newdata/adclick?ad=674146&bl=80802&ct=adpreview&st=16&in=YK2NFgCJu2FWSQoAjkkKAIhJCgBhSQoAfkkKAGJJCgA%3D&ag=19",
            "id": "674146"
        }]
    }

    //console.log(JSON.parse(json));

    jQuery.each(JSON.parse(json), function(idx, obj) {
       alert(idx + '__' + obj.news.title);
    });
})($);

因此,控制台显示以下错误消息:

  

未捕获的SyntaxError:意外的令牌o

您可以看到代码jsfiddle

的在线版本

2 个答案:

答案 0 :(得分:2)

将您的最后一位更改为 -

    jQuery.each(json.news, function(idx, obj) {
           alert(idx + '__' + obj['title']);
        });
    })($);

你必须迭代json的新闻

答案 1 :(得分:0)

它已经被解析了。所以JSON.parse会抛出错误。问题解答JSON.parse(json)应该很简单json

希望这有帮助。