Here is my json:

{ "items": [{ "id" : 1, "name": "Bill Gates", "age": 43 }, { "id" : 2, "name": "Sergey Brin", "age": 33 }, { "id" : 3, "name": "Larry Page", "age": 34 },{ "id": 4, "name":"dsfsdfs", "age":34 }] } 

 <script type="text/javascript"> $(function () { $.getJSON('/test2/obj/data/base.json', function (data) { // alert(data); for (var key in data['items']) { alert(" "+ key + " "+data[key]); } }); }); </script> 

How do i get an array in js? Parse does not roll, also need for to access elements. You need to drive this JSON into the table, and that's it!

Understood! stack.js - loads JSON - generates a table - gives it to index.php

  $('document').ready(function(){ loadData(); }); function loadData() { //загружаю на страницу $.getJSON('objects.json', function (data) { // console.log(data); var out = ''; out+='<table class="table table-hover">'; out+='<thead>'; out+='<tr>'; out+='<th>Формат</th>'; out+='<th>Адрес</th>'; out+='<th>Завод</th>'; out+='<th>№ телефона заведующего</th>'; out+='<th>№ городского телефона </th>'; out+='<th> ФИО заведующего</th>'; out+='</tr>'; out+='</thead>'; out+='<tbody>'; for (var key in data){ out+='<tr>'; out+='<td>'+data[key]['format']+'</td>'; out+='<td>'+data[key]['adress']+'</td>'; out+='<td>'+data[key]['factory']+'</td>'; out+='<td>'+data[key]['phone1']+'</td>'; out+='<td>'+data[key]['phone2']+'</td>'; out+='<td>'+data[key]['fio']+'</td>'; out+='</tr>'; } out+='</tbody>'; out+='</table>'; $('#items').html(out); }) } 

JSON objects.json itself

 { "101" : { "format" : "АЗП-1", "adress" : "г. Скидель, ул. Ленина, 78Б", "factory" : "2302", "phone1" : "8-029-117-17-75", "phone2" : "", "fio" : "Стокиш Светлана Михайловна" }, "102" : { "format" : "АЗП-2", "adress" : "г. Скидель, ул. Ленина, 4", "factory" : "2303", "phone1" : "8-029-327-47-34", "phone2" : "", "fio" : "Сакович Тамара Анатольевна" }, "103" : { "format" : "АЗП-3", "adress" : "г. Скидель, ул. Кирова, 3а", "factory" : "2304", "phone1" : "8-044-585-88-48", "phone2" : "", "fio" : "Мулярчик Наталья Дмитриевна" } } 

And index.php

 <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); $APPLICATION->SetTitle("Торговые объекты"); ?> <head> <link rel='stylesheet' href='css/bootstrap.css'> </head> <body> <div id="items"></div> <script src="js/jquery-3.2.1.min.js" async></script> <script src="js/stack.js" async></script> </body> <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?> 
  • JSON.parse than not happy? - Rostyslav Kuzmovych
  • I could not use parse, because there was an error Unexpected token in JSON at position 1 at JSON.parse (<anonymous>) - ZIBER MINSK

1 answer 1

The JSON.parse () method parses the JSON string. There is a JSON.stringify () method. It works the other way around - converts the JavaScript value to a JSON string.

 $.getJSON('/test2/obj/data/base.json', function (data) { const responseData = JSON.parse(data); for (var key in responseData['items']) { alert(`Key: $(key), name: $(data[key].name), age: $(data[key].age).`); } }); 
  • Thank! but that's what the alert displays. SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) I can not understand why the alert is so decorated. Can you tell? - ZIBER MINSK pm
  • JSON.parse (data); does not want to parse json. The format in base.json is correct - ZIBER MINSK
  • $.getJSON independently parses json. In the template line, you use data[key] instead of responseData['items'][key] and incorrect brackets (round instead of curly) - vp_arth