user1063287
user1063287

Reputation: 10869

Correct format for JSON file being accessed via getJSON()?

.json file

{"object_name":
    [
        {"key1":"value1","key2":"value2"}
        {"key1":"value3","key2":"value4"}
    ]
}

jQuery

<script>
$(document).ready(function() {
    $("#my_id li a").click(function(){
        $.getJSON("http://path/to/json.json", function(my_results){  
            console.log(my_results.object_name[0].key2);
        });
    });
});
</script>

I was expecting that this would output:

The value of key2, in the first object, in the object_name array.

But it's not outputting anything to the console.

What is incorrect in the above code?

Upvotes: 0

Views: 576

Answers (3)

Matthew Daly
Matthew Daly

Reputation: 9476

Looks to me like your JSON is missing a comma between the two rows. Try running it through JSONLint to check it.

Upvotes: 1

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7668

Place a comma(,) in your object_name

For JSon validators you can visit this link

{"object_name":
    [
        {"key1":"value1","key2":"value2"},
        {"key1":"value3","key2":"value4"}
    ]
}

Upvotes: 0

Roger
Roger

Reputation: 1082

try:

{"object_name":
[
{"key1":"value1","key2":"value2"},
{"key1":"value3","key2":"value4"}
]
}

Upvotes: 0

Related Questions