简体   繁体   中英

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.

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.

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

like this

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

JSFIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM