0

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.

asked Nov 2, 2015 at 21:01

2 Answers 2

1

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

like this

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

JSFIDDLE

answered Nov 2, 2015 at 21:04
Sign up to request clarification or add additional context in comments.

3 Comments

How can I change my array to make something like data['lang']['SECTION_NAME'].english ? Because, if my son change, I need to change all the [x]...
then object design should be { lang : { section : { english : 'My title' } } @roberto
oh my bad :( , { lang : { SECTION_NAME : { english : 'My title' } } . now try :) @roberto
1

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.

answered Nov 2, 2015 at 21:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.