stackow7
stackow7

Reputation: 3

To print an array?

please help lead to the title of the json-array   my attempt:

(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);
    });
})($);

As a result, the console displays the following error message:

Uncaught SyntaxError: Unexpected token o

you can see the online version of the code jsfiddle

Upvotes: 0

Views: 52

Answers (2)

el3ien
el3ien

Reputation: 5405

Change your last bit to -

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

You have to iterate news of json

Upvotes: 2

UndefinedN
UndefinedN

Reputation: 23

It is already parsed. So JSON.parse will throw an error. The fix is JSON.parse(json) should be simple json.

Hope this helps.

Upvotes: 0

Related Questions