1

I am trying to parse the following freebase API in jQuery.

https://www.googleapis.com/freebase/v1/search?query=us%20president&filter=(all%20type:/people/person)&output=(/common/topic/image)

But I've a problem when I try to access /common/topic/image "mId" for image .

Can any body tell me what is the right way to access objects or arrays which containg "/common/topic/image" these type of keys.

Thanks!

2 Answers 2

1
$.getJSON("https://www.googleapis.com/freebase/v1/search?query=us%20president&filter=(all%20type:/people/person)&output=(/common/topic/image)", function (data) {
    var r = data.result; 
    // iterate each result   
    $.each(r, function (i, j) {
        var arr = j.output['/common/topic/image'];
        //iterate each arr['/common/topic/image']
        $.each(arr['/common/topic/image'], function (k, l) {
            console.log(l);
        });
    });
});

See JSFiddle

From your comments,

If you check your json, last result has no array called /common/topic/image.

So when it tried to iterate, it failed[Unable to get the length].

So just add an condition to it like if (arr['/common/topic/image']) {...}. Now if it has empty element it will be skipped.

Check this fiddle http://jsfiddle.net/praveen_jegan/6MCKA/2/

Sign up to request clarification or add additional context in comments.

7 Comments

Thanx Praveen Jeganathan.
@ARV I'm glad I was able to help, my friend.
hello Praveen Jeganathan i am getting error that" Uncaught TypeError: Cannot read property 'length' of undefined " in $.each(arr['/common/topic/image'], function (index, image) {}
@ARV if you check the last result in json, there no array called /common/topic/image. So when it tried to iterate, it failed. So just add an condition to it like ` if (arr['/common/topic/image']) {...}`. Now it has empty element it will be skipped. Now check this fiddle jsfiddle.net/praveen_jegan/6MCKA/2. Hope you understand.
thanx Praveen Jeganathan. Now it is working fine. Thancx for your guidance.
|
0

If object key contain dots or slashes, you can access it in this way:

var o = {
 'some.key': 123,
 'another/key': 321
};

console.log(o['some.key'], o['another/key']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.