0

I have this JSON:

{
    "fakultaeten": [
        {
            "id": "1",
            "name": "Carl-Friedrich Gauß",
            "institut": [
                {
                    "Mathematik": [
                        {
                            "Verbrauch": "852 kWH",
                            "Effizienz": "5,5"
                        }
                    ],
                    "Informatik": [
                        {
                            "Verbrauch": "852 kWH",
                            "Effizienz": "5,5"
                        }
                    ],
                    "Wirtschaftswissenschaften": [
                        {
                            "Verbrauch": "852 kWH",
                            "Effizienz": "5,5"
                        }
                    ],
                    "Sozialwissenschaften": [
                        {
                            "Verbrauch": "852 kWH",
                            "Effizienz": "5,5"
                        }
                    ]
                }
            ],
            "verbrauch": "852 kWH"
        }
    ]
}

And I want to create list of all items in "institut", like this:

  • Mathematik
  • Infomratik
  • Wirtschaftswissenschaften
  • etc

I'm trying this:

$.each(data.fakultaeten, function(key,value)
            {
                var mother = "<li id='first'>"+value.name+"<ul>";
                $.each(value.institut, function(key1, value1)
                {
                // create the list here
                });
             })

The result is only: [object Object]

What is wrong with my solution?

5
  • 1
    Do you really have JSON? Or do you just have a javascript object? Commented Aug 2, 2013 at 13:54
  • 1
    I don't think it is, if you are getting data from an async call it is likely a javascript object. JSON can only ever be a string, if you have a string then you should parse it with JSON.parse(jsonString) Commented Aug 2, 2013 at 14:03
  • I do that, but didn't put it here, because it's not part of my question. Commented Aug 2, 2013 at 14:05
  • you said "I have this JSON"... if you already have it as an object, and don't want to show the parsing, then no need to mention JSON. You just confuse the situation. Besides, what part of you code doesn't work? Commented Aug 2, 2013 at 14:06
  • 1
    Anyways, I just added the information JSON, because I thought it MIGHT be important. If it's not - ignore it :) Commented Aug 2, 2013 at 14:07

2 Answers 2

1

Your problem is that institut is an array containing a single object, so you can reference that single object using institut[0].

This will work:

$.each(data.fakultaeten, function (key, value) {
    var mother = "<li id='first'>" + value.name + "<ul>";
    $.each(value.institut[0], function (key1, value1) {
        alert(key1);
    });
});

Here is a working example

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

Comments

0

Try this:

$.each(data.fakultaeten, function (key, value) {
    var mother = "<li id='first'>" + value.name + "<ul>";
    $.each(value.institut[0], function (key1, value1) {
        console.log(key1); // key1 gives you output that you want
    });
});

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.