Good evening! I solve the problem of forming a tree list.

There is for example such an array:

$mas = array ( "City" => array ( "a" => "New-York", "b" => "London", "c" => "Moscow" ), "Numbers" => array ( "element-1", "element-2", "element-3", "element-4", "element-5", "element-6" ), "Other" => array ( "one", 5 => "two", "three", 7=> array ("mas10", "mas15") ) ); 

After conversion to JSON-format, the array looks like this:

 { "City":{"a":"New-York","b":"London","c":"Moscow"}, "Numbers" ["element-1","element-2","element-3","element-4","element-5","element-6"], "Other":{"0":"one","5":"two","6":"three","7":["mas10","mas15"]} } 

Through JSON, I pass it to the JS code, which has a function that lists the tags:

 function Dump(d,l) { if (l == null) l = 1; var s = ''; if (typeof(d) == "object") { s += "<li><img src='../images/folder.gif' />"; for (var k in d) { s += "<ul>" + Dump(d[k],l+1)+"</ul>"; } s += "</li>"; } else { s += "<li><img src='../images/file.gif' />" + d + "</li>"; } return s; } str = Dump(data,1); $("#span2").html(str); 

And the whole thing is formed in this form: http://www.imgup.ru/image-12jrx7161953.html

And I would like to make the headings of elements that are arrays in turn (such as "City" or "7") show their headings, and elements that simply store values ​​do not show their headings (such as the key "a", "5" or simply values ​​without a specified key as for example "element-3", "mas15", etc.). How to do it? I tried to print the values ​​of k in a loop, but it displays all the keys, even if they are not arrays.

  • json as a string put here for convenience. - zb '
  • Ie how to convert to a string, and then disassemble it by bone? I think it will be even more difficult. I just don’t know how to distinguish between keys that contain an array and just values. Here you need to add one condition, if the key contains a subarray, then output the name of the key, otherwise omit its name. Is there such a function in JS? Tell me, experts! - IntegralAL

2 answers 2

Finished my code as follows:

 function Dump(d,l,name) { if (l == null) l = 1; var s = ''; if (typeof(d) == "object") { s += "<li><img src='../images/folder.gif' />"+name; for (var k in d) { s += "<ul>" + Dump(d[k],l+1,k)+"</ul>"; } s += "</li>"; } else { s += "<li><img src='../images/file.gif' />" + d + "</li>"; } return s; } str = Dump(data,1, "Корень"); $("#span2").html(str); 

And now the JSON structure obtained from the php-file has the following form, which I needed: http://www.imgup.ru/image-12knx501155.html

    If JS comes your JSON ready? then I would advise you to use this array brute force construction on jQuery

     var URL = 'file.json' $.getJSON(URL, function(result){ $.each(result, function(key, value){ // Здесь мы получим три значения результат => ключ => значение } ) } ) 

    Please do not kick, maybe I did not understand your question.

    • Yes, thanks for the advice! At first, I also used $ .each to iterate over the elements of the array, unfortunately such methods do not take into account the level of nesting of values ​​(since I have multidimensional and multilevel arrays). And then I have to distinguish between keys that contain subarrays and keys that contain just values, because it does not fit unfortunately. - IntegralAL