There is a file cakes.json and in its content:

  { "cake": [ {"title":"Cake1","price": "cake price","description":"cake price"}, {"title":"Cake2","price": "cake price","description":"cake price"}, ] }; 

You need to write it to the variable var cakes

So that when you call alert(cakes.cake[1].title);

the name of the first cake was displayed from the file cakes.json

Tried manipulations of $.getJSON('js/test.json', function(data)

but failed.

  • 2
    you have an error in json after the second object after "cake price"} there should not be a comma ..... and in. $.getJSON('js/test.json' should also be cakes.json ?)). ....... and add the entire getJSON function along with your callback - Alexey Shimansky
  • Paste your json here json.parser.online.fr and you will see that it breaks ...... or even write JSON.parse(ВАША_СТРОКА_JSON); in the console JSON.parse(ВАША_СТРОКА_JSON); and you will also get an error .............. semicolon should not be at the end either - Aleksey Shimansky
  • one
    I did not speak about the перед ключом description ..... I spoke после второго объекта после cake price ..... that is, after the line with the object {"title":"Cake2","price": "cake price","description":"cake price"} must not be a comma. since there is nothing after her anymore - Aleksey Shimansky
  • one
    Judging by the unsuccessful getJSON, there is a suspicion that this is again a problem with asynchrony :) - Pavel Mayorov
  • one

1 answer 1

If you need the first element, you can do so.

 var json = { "cake": [ {"title":"Cake1","price":"cake price","description":"cake price"}, {"title":"Cake2","price":"cake price","description":"cake price"} ] }; console.log(json.cake[0].title) 

but for good you need to understand what exactly you need to say how to do it