user4072347
user4072347

Reputation:

Print a JSON data with Javascript

I have the following JSON.

{
    "lang": [
        {
            "SECTION_NAME": {
                "english": "My title"
            },
            "SECTION_NAME_2": {
                "english": "My title"
            }
        }
    ]
}

And I'm looking to print the value like this:

$.getJSON('json/lang.json', function(data) {
    var text = data['lang']['SECTION_NAME'];
    $('#title').html(text.english);
});

But I have the following error:

TypeError: undefined is not an object (evaluating 'text.english')

Any help please.

Thanks.

Upvotes: 0

Views: 53

Answers (2)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You have to access it via index as lang is an array of object

like this

console.log(data['lang'][0]['SECTION_NAME'])

JSFIDDLE

Upvotes: 1

Quentin
Quentin

Reputation: 943537

The value of lang is an array which contains an object.

You are ignoring the array and trying to access the objects as if it was the value of lang directly.

Upvotes: 1

Related Questions