-1

I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.

var error = [
    {
        "LessonName":"Understanding Multiplication",
        "LessonID":"13343",
        "no of questions":[{"Locked":"31","Unlocked":5}]
    },

    {
        "LessonName":"Finding Unknown Values ",
        "LessonID":"13424",
        "no of questions":[{"Locked":"34","Unlocked":5}]
    }
]


function jsd(){
    document.write(error[0].LessonName);
    document.write(error[0].'no of questions'[0].Locked);
}

document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.

deceze
  • 491,798
  • 79
  • 706
  • 853
  • use parseJson for it. – priya_singh Oct 06 '16 at 10:19
  • Link to other Stack Overflow response (brackets) http://stackoverflow.com/questions/10311361/accessing-json-object-keys-having-spaces – stefan_ah Oct 06 '16 at 10:20
  • 1.**First** of all don't make space between the keys."no of questions". 2. **Secondly** the `error[0].Locked` doesn't exists. error[0]["no of questions"][0].Locked Does exists – Javasamurai Oct 06 '16 at 10:27

2 Answers2

2

You may use a property accessor with brackets for the string.

error[0]['no of questions'][0].Locked
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
0

You must use this syntax for strings with spaces.

document.write(error[0]['no of questions'][0].Locked);
Rob
  • 127
  • 8