Добрый день. Есть у нас скрипт, возвращающий JSON-данные. оттакие, например: [{"attributes":{"data":"type: 'file'","class":"leaf","id":"a-1"},"data":"atr-1","icons":"url(img/g.png)"},{"attributes":{"data":"type: 'file'","class":"leaf","id":"a-2"},"data":"atr-2","icons":"url(img/g.png)"},{"attributes":{"data":"type: 'folder', valid_children: ['file']","state":"open","class":"open","id":"g-1"},"data":"group-1","icons":"url(img/f.png)","children":[{"attributes":{"data":"type: 'file'","class":"leaf","id":"sa-1"},"data":"sub-atr-1","icons":"url(img/g.png)"},{"attributes":{"data":"type: 'file'","class":"last leaf","id":"sa-2"},"data":"sub-atr-2","icons":"url(img/g.png)"}]},{"attributes":{"data":"type: 'folder', valid_children: ['file']","state":"open","class":"open","id":"g-2"},"data":"group-2","icons":"url(img/f.png)","children":[{"attributes":{"data":"type: 'file'","class":"leaf","id":"sa-3"},"data":"sub-atr-3","icons":"url(img/g.png)"},{"attributes":{"data":"type: 'file'","class":"last leaf","id":"sa-4"},"data":"sub-atr-4","icons":"url(img/g.png)"}]},{"attributes":{"data":"type: 'folder'","class":"open last"},"data":"1133","icons":"none","children":[{"attributes":{"data":"type: 'file'","class":"leaf last","id":"a-3"},"data":"atr-3","icons":"url(img/g.png)"}]}] или в красивом виде вот так: Код (Text): '0' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "leaf" 'id' => "a-1" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "atr-1" 'icons' => "url(img/g.png)" '1' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "leaf" 'id' => "a-2" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "atr-2" 'icons' => "url(img/g.png)" '2' ... 'attributes' ... 'data' => "type: 'folder', valid_children: ['file']" 'state' => "open" 'class' => "open" 'id' => "g-1" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "group-1" 'icons' => "url(img/f.png)" 'children' ... '0' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "leaf" 'id' => "sa-1" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "sub-atr-1" 'icons' => "url(img/g.png)" '1' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "last leaf" 'id' => "sa-2" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "sub-atr-2" 'icons' => "url(img/g.png)" '3' ... 'attributes' ... 'data' => "type: 'folder', valid_children: ['file']" 'state' => "open" 'class' => "open" 'id' => "g-2" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "group-2" 'icons' => "url(img/f.png)" 'children' ... '0' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "leaf" 'id' => "sa-3" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "sub-atr-3" 'icons' => "url(img/g.png)" '1' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "last leaf" 'id' => "sa-4" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "sub-atr-4" 'icons' => "url(img/g.png)" '4' ... 'attributes' ... 'data' => "type: 'file'" 'class' => "last leaf" 'id' => "a-3" 'undefined' => "undefined" 'getNamedItem' => "undefined" 'setNamedItem' => "undefined" 'removeNamedItem' => "undefined" 'item' => "undefined" 'getNamedItemNS' => "undefined" 'setNamedItemNS' => "undefined" 'removeNamedItemNS' => "undefined" 'data' => "atr-3" 'icons' => "url(img/g.png)" нужно из этого оставить только все значения attributes.id и data то есть должно остаться что-то типа: Код (Text): '0' ... 'attributes' ... 'id' => "a-1" 'data' => "atr-1" '1' ... 'attributes' ... 'id' => "a-2" 'data' => "atr-2" '2' ... 'attributes' ... 'id' => "g-1" 'data' => "group-1" 'children' ... '0' ... 'attributes' ... 'id' => "sa-1" 'data' => "sub-atr-1" '1' ... 'attributes' ... 'id' => "sa-2" 'data' => "sub-atr-2" '3' ... 'attributes' ... 'id' => "g-2" 'data' => "group-2" 'children' ... '0' ... 'attributes' ... 'id' => "sa-3" 'data' => "sub-atr-3" '1' ... 'attributes' ... 'id' => "sa-4" 'data' => "sub-atr-4" '4' ... 'attributes' ... 'id' => "a-3" 'data' => "atr-3" глубина вложений элемент |-элемент не более. Тут наверняка функция строчек на 10, но как ее писать не знаю. Помогите пожалуйста. примерный код, но это будет просто строка. function print_r(obj) { var txt = ""; for(var i=0;i<99;i++) { if(obj.children) { txt += obj.children.attributes.id + obj.children.data + + "\n"; if(obj.children.children) txt += print_r(obj.children); } } return txt; } alert(print_r(obj));
eval(JSONstring) превращает строку в объект .Есть специальная функция в JS для работы с этим форматом, просто точно не помню, как она пишется. Далее циклами выводятся ненужные параметры. Что почитать: https://developer.mozilla.org/en/Core_J ... rray/slice https://developer.mozilla.org/En/Core_J ... ray/Splice http://javascript.ru/tutorial/object/in ... ie-svoistv https://developer.mozilla.org/en/Core_J ... ray_Object