0

I'm generating a json file with php and store it on my server. Here is the code for exporting the json

/**
 * FUNCTIONS TO EXPORT AS JSON
 */
public function expose() {
    return array(
             'guid' => $this->guid,
               'title' => $this->title,
               'folder' => $this->folder,
               'owner' => $this->owner,
                 #'pictures' => json_encode(array_values($this->pictures), JSON_FORCE_OBJECT),
                 'excude' => $this->excude,
                 'added' => $this->added,
                 'lastViewed' => $this->lastViewed,
        );
    #return get_object_vars($this);
}

public function toJSON(){
    return json_encode($this->expose(), JSON_FORCE_OBJECT);
}

The content of the file is this:

{"guid":"","title":"Sample Gallery","folder":"sampleGallery","owner":"","excude":true,"added":"","lastViewed":""}

then in my html file I try to load this with jquery, but I fail to get the objects to the console

$.getJSON('/files/galleries/index/sampleGallery.gallery', function(data) {
    console.log(data); // works!
var jsonObj = jQuery.parseJSON(data); 
for (key in jsonObj) {
    console.log(key+':'+JSON.stringify(jsonObj[key]));
}
});

Note that the loading of json works fine!

Can someone tell me what I am doing wrong? Thanks in advance!

3
  • you are parsing a javascript object.. no need to parse it since already is a javascript object Commented Jan 30, 2013 at 15:17
  • Are you sure that the path to your sampleGallery.gallery file is correct in regards to the www server? Commented Jan 30, 2013 at 15:18
  • yes, the server returns a correct json file works now with removing the line jQuery.parseJSON(data); Commented Feb 1, 2013 at 15:07

7 Answers 7

1

You don't need to call parseJSON(), beacase data is already an object.

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

Comments

0

If you are returning the data as JSON, you don't need to parse it again. JQuery has already done that for you.

$.getJSON('/files/galleries/index/sampleGallery.gallery', function(data) {
    console.log(data); // works!
    for (key in data) {
        console.log(key+':'+JSON.stringify(data[key]));
    }
});

Comments

0

I think your data is already parsed because you call getJSON, you should access directly the data object and avoid the jQuery.parseJSON(data); call.

Comments

0

You don't have to do var jsonObj = jQuery.parseJSON(data);. jQuery.getJson parses the returned value as a json object before calling the success callback.

Your code should be

$.getJSON('/files/galleries/index/sampleGallery.gallery', function(data) {
    console.log(data); // works!
    for (key in data) {
        console.log(key+':'+JSON.stringify(data[key]));
    }
});

Comments

0

Presumably data contains parsed JSON (i.e. a JavaScript object).

Trying to parse a JavaScript object as if it were a string containing JSON isn't going to work.

Get rid of var jsonObj = jQuery.parseJSON(data); and then for (var key in data) {

Comments

0

data is already an object, no need to parse it.

Just read the manual: http://api.jquery.com/jQuery.getJSON/

Comments

0

You don't need to do jQuery.parseJSON(data) any more, since you are using getJSON.

Check out the documentation here: http://api.jquery.com/jQuery.getJSON/

I think your code should be:

$.getJSON('/files/galleries/index/sampleGallery.gallery', function(data) {
    console.log(data); // works!
// jsonObj removed, just use data since data is in JSON already
for (key in data) {
    console.log(key+':'+JSON.stringify(jsonObj[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.