oskarno oskarno - 1 year ago 80
jQuery Question

How to access values in a multidimensional JSON array with jQuery

So I fetch a multidimensional JSON array with getJSON and I want to access values in the array, but can't reach longer than the first element in the array.

"address":" Grenv\u00e4gen 1-3",
"zipcode":" 577 39",
"district":" Hultsfred",

...and so on.

My code:

$.getJSON("/url/producers.json", function(data) {
$.each(data, function() {
$.each(this, function(key, value) {

The output is an object, I don't know any better way to paste it, copied from the browsers console: "
> Object {id: "1", name: "Emåmejeriet", address: "Grenvägen 1-3, zipcode: "577 39", disctrict: "Hulsfred"...}

I've searched and tested different approaches as I said, from this forum, but couldn't get my head around it.

Answer Source

If the output of value.producer is an object, then you should be able to access the properties of that object. For example, value.producer.name should output 'asdfsadf'.


It looks as though your json data is not valid. (I'm assuming "producers" is a property of the data object and the value of that property is an array of all of the "producer" objects.)

I've created this fiddle ( http://jsfiddle.net/kAsPm/ ) and it works as expected. I just had to change the comma to a colon after "producers":

"producers":  // changed from "producers",