2

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.

"producers",
[
    {
        "producer":{
            "id":"1",
            "name":"Em\u00e5mejeriet",
            "address":" Grenv\u00e4gen 1-3",
            "zipcode":" 577 39",
            "district":" Hultsfred",
            "webpage":"http:\/\/www.emamejeriet.se",
            "logoURL":"..\/producenter\/images\/ema.png",
            "latitude":"57.4999",
            "longitude":"15.828"
        }
    },
    {
        "producer":{
            "id":"2",

...and so on.

My code:

$.getJSON("/url/producers.json", function(data) {
    $.each(data, function() {
        $.each(this, function(key, value) {
            console.log(value.producer);
        });
    });
});

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.

2
  • 2
    Can you post the actual JSON? Hard to tell without seeing that ... Commented Dec 19, 2012 at 19:46
  • The output you show as an example makes no sense whatsoever. Please show some actual data in a code block. Commented Dec 19, 2012 at 19:48

3 Answers 3

4

Open your firebug's console, paste the following and execute:

var json = '{' + 
'"peopleList":[' +
'{"id":1,"name":"marcelo","address":"rua meh","phone":"85 99999999"},'+
'{"id":2,"name":"marcelo2","address":"rua meh2","phone":"85 9999.9999"}]}';

var parsed = JSON.parse(json);

Now, as you will have two objects under the second level of your JSON, you need to specify the object and then you will see that it will allow you to select its properties:

parsed.peopleList[0].name

Before working with the dynamically generated JSON, get some dummy data for testing purposes (with the same structure, of course) and play with it before implementing the final code.

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

1 Comment

I usually don't work in Firebug, although it seems to have some nice features, like this. Worked perfect, thanks for pointing that out.
3

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'.

UPDATE

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",
[
    {
        "producer":{
            "id":"1",
            ...

3 Comments

That is exacly what I've been trying to do, but I get "Undefined".
It seems like the data is invalid. I've updated my answer with a jsfiddle linked.
Oh, okay. I went and updated my function that created the JSON file and just returned it with the producers to test it out, not multidimensional and that did the trick. I don't actually know why I did it multidimensional in this case. Thanks!
1

I used tempo.js rendering engine, it does multidimensional array rendering. here is the link http://tempojs.com/

download tempo.cs and follow the link .

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.