3

I'm struggling to retrieve some value on a json in this url:

http://go-gadget.googlecode.com/svn/trunk/test.json

the url data looks like this:

[
{
    "key":{
        "parentKey":{
            "kind":"user",
            "id":0,
            "name":"test 1"
        },
        "kind":"smsgateway",
        "id":5707702298738688
    },
    "propertyMap":{
        "content":"test1 content",
        "date":"Dec 12, 2013 2:58:57 PM",
        "user":"test1"
    }
}]

By ignoring the "key", I want to access the value of "propertyMap" object (content,date and user) using javascript code. I have tried this code but it couldn't get the result:

var url = "http://go-gadget.googlecode.com/svn/trunk/test.json";
$.getJSON(url, function (json) {
for (var i = 0; i < json.length; i = i + 1) {
    var content = json[i].propertyMap.content;
    console.log('content : ', content);
    var user = json[i].propertyMap.user;
    console.log('user: ', user);
    var date = json[i].propertyMap.date;
    console.log('date : ', date);
    }
    });

(unsuccessful code here http://jsfiddle.net/KkWdN/)

considering this json can't be change, is there any mistake I've made from the code above or there's any other technique to get the result?

I just learn to use javascript and json for 1 month so response with an example is really appreciated.

--edited: I change [].length to json.length, now I'm looking for the answer to access the url

2
  • [] creates an empty array and [].length gets the length of an empty array, which of course if 0. Commented Dec 12, 2013 at 18:48
  • No, not possible due to the same origin policy. You'll have to instead retrieve the json using your server or some other server as a proxy. Commented Dec 12, 2013 at 18:49

4 Answers 4

1

That would be something like :

$.getJSON("http://go-gadget.googlecode.com/svn/trunk/test.json", function(json) {
    for (var i = 0; i < json.length; i++) {
        var map     = json[i].propertyMap;
        var content = map.content;
        var user    = map.user;
        var date    = map.date;

        $('#date').text(date);
        $('#nohp').text(user);
        $('#content').text(content);
    }
});

But the request fails, as no 'Access-Control-Allow-Origin' header is present on the requested resource, so you're being stopped by the same origin policy

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

5 Comments

thank you for the answer, its really help me. can you suggest the way so I can access the request?
You can't, it's a json file, not a service, so it doesn't really support jsonp or CORS, so your only option is to use a serverside language, like PHP, to get the data.
I have another json here, the format looks like what I explain before. is that possible to access?
Nope, that's the same, no CORS headers, and doesn't appear to support JSONP
Ok, I'll try to use PHP instead. thank you so much for helping :)
1

What do you think [].length; would evaluate to .

It is 0 , so it would never go inside the for loop.

Other than that you code looks ok.

Replace your for loop as below

$.getJSON(url, function (json) {
    for (var i = 0; i < json.length; i = i + 1) {

Also you seem to be accessing a API as part of the different domain. So you need to use CORS or JSONP to get this working if you want to retrieve data from a different domain.

2 Comments

so that's why I never gonna get to the url. can you suggest some example using jsonp? thank you before :)
@sabrina: JSONP has to be supported by the server as well.
0

Change:

for (var i = 0; i < [].length; i = i + 1) {

to

for (var i = 0; i < json.length; i = i + 1) {

DEMO here.

Comments

0

How about using something like the following In your case, say the object is myObj, I would get the value like this

var content = fetchValue(myObj, [0, "propertyMap", "content"], "");
var date = fetchValue(myObj, [0, "propertyMap", "date"], new Date());
var user = fetchValue(myObj, [0, "propertyMap", "user"], "");

Just to make sure that we send a default value in case we do not get the desired ojbect. The beauty of this approach is that now you do not have to worry about array or objects nested in the structure. The fetchValue function could be something like below.

function fetchValue(object, propertyChain, defaultValue){
    var returnValue;
    try{
         returnValue = object;
         forEach(propertyChain, function(element){
            returnValue = returnValue[element];
         });
    }catch(err){
        return defaultValue;
    }
    if(returnValue == undefined) {
        returnValue = defaultValue;
    }
    return returnValue;
}

Adding the forEach function

function forEach(array, action){
    for(var x in  array)
        action(array[x]);
}

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.