user4099673
user4099673

Reputation:

Load content from json file failed

I have the following structure for my json file:

{
    "activity": [
        {
            "fr": "belvedere",
            "en": "scenic-point"
        }
    ],
    "category": [
        {
            "fr": "repertoire-attraits",
            "en": "attractions-directory"
        }
    ],
}

I have a select with several option. With on change, I load the json file and would like to get the values:

$('#add').on('change', function() {
    $.getJSON(this.value, function(json) {
        alert(json.activity.fr)
    });
});

But it doesn't works.

It load the json file but I do not have any alert with the activity name...

Any ideas why ?

Thanks.

Upvotes: 0

Views: 34

Answers (2)

Limeoats
Limeoats

Reputation: 426

You need to get rid of the comma after your last ']'. A comma should only follow a ']' when there will be another item after it.

This code will work:

{
"activity": [
    {
        "fr": "belvedere",
        "en": "scenic-point"
    }
],
"category": [
    {
        "fr": "repertoire-attraits",
        "en": "attractions-directory"
    }
]

}

Upvotes: 1

Brian Glaz
Brian Glaz

Reputation: 15696

If you use a JSON linter like http://jsonlint.com/, you will see that it's not valid JSON. You can't have a trailing ,. That's fine in PHP arrays, but not JSON.

You will probably also want to use the jquery $(this).val() rather than this.value

Upvotes: 1

Related Questions