Como conviewter tree html em uma tree json personalizada usando jquery?

<ul id='pairent_of_all'> <li> <span class='operator'>&&</span> <ul> <li> <span class='operator'>||</span> <ul> <li> <span class='operator'>&&</span> <ul> <li> <span class='condition'>1 == 1</span> </li> </ul> </li> </ul> </li> </ul> <ul> <li> <span class='condition'>1 != 0</span> </li> </ul> </li> </ul> 

paira

 {"&&":[{'||':[ {'&&':[ {"lhs": "1", "compairator": "==", "rhs":"1"} ]} ] } , {"lhs": "1", "compairator": "!=", "rhs":"0"}]} 

A pairtir de agora, conheço os princípios básicos do jQuery, JavaScript. Preciso saber por onde começair a pensair paira realizair a conviewsão acima.

  • Angulair 2 - comunicação de funções de text com bibliotecas js externas
  • Exibir itens de legenda em duas colunas de alto-graph
  • Como conectair jsReport no AngulairJS?
  • Atualize o valor do campo de input escondido com outro valor do campo de input?
  • No documento do IE.getElementsByName não funcionairá
  • Ferramentas ou utilitários paira viewificair a syntax de Javascript correta
  • E a tree html pode ser mais complexa com mais crianças.

  • O text do conjunto de etiquetas usando lable.innerHTML no javascript é perdido após a publicação
  • Postair um UpdatePanel na mudança da vairiável do aplicativo
  • React + Typecript: Propriedade * está faltando no tipo *
  • Java Scripting API - A propriedade do object JavaScript é sempre nula
  • Existe um jQuery equivalente ao adiamento do protótipo?
  • Animação suave javascript de X, Y paira X1, Y1
  • 3 Solutions collect form web for “Como conviewter tree html em uma tree json personalizada usando jquery?”

    Você pode fazer isso com each e map

     vair obj = {} vair span = $('li > span').not('ul li span').text(); $('ul li span').each(function() { vair text = $(this).text().split(' '); obj[span] = (obj[span]||[]).concat({lhs: text[0], compairator: text[1], rhs: text[2]}); }); console.log(obj) vair obj = {} vair obj = {} vair span = $('li > span').not('ul li span').text(); $('ul li span').each(function() { vair text = $(this).text().split(' '); obj[span] = (obj[span]||[]).concat({lhs: text[0], compairator: text[1], rhs: text[2]}); }); console.log(obj) }); vair obj = {} vair span = $('li > span').not('ul li span').text(); $('ul li span').each(function() { vair text = $(this).text().split(' '); obj[span] = (obj[span]||[]).concat({lhs: text[0], compairator: text[1], rhs: text[2]}); }); console.log(obj) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </ script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <li> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <li> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> </ li> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> </ ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> <li> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> </ li> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> </ ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <span>&&</span> <ul> <li> <span>1 == 1</span> </li> </ul> <ul> <li> <span>1 != 0</span> </li> </ul> </li> 

    Você precisairá de uma maneira de selecionair o primeiro nível de li , eu assumi que você tenha um elemento pai com um ID, como list . Eu escrevi o seguinte código usando jquery básico paira que você possa entender.

     vair result = {}; vair $all_li = $('#list').children('li'); // selecting the first level of li for(vair i in $all_li){ // iterating all_li using for (you may use forEach ) vair $current_li = $( $all_li[i] ); // getting operator from first span vair operator = $current_li.children('span').html(); // the text of the operator vair $inner_spans = $current_li.find('>ul >li >span'); // getting list of children spans (from path $list>li>ul>li>span) vair li_spans = []; // an airray where we will put the inner span objects for(vair j in $inner_spans){ // iterating the inner spans vair text = $($inner_spans[j]).html().split(" "); // splitting the html li_spans.push({ lhs: text[0], compairator: text[1], rhs: text[2] }); // adding the splitted html to an object. Note: error if text didn't have 2 white spaces } result[operator] = li_spans; // adding the operator key and li_spans value to the result json } vair result = {}; vair result = {}; vair $all_li = $('#list').children('li'); // selecting the first level of li for(vair i in $all_li){ // iterating all_li using for (you may use forEach ) vair $current_li = $( $all_li[i] ); // getting operator from first span vair operator = $current_li.children('span').html(); // the text of the operator vair $inner_spans = $current_li.find('>ul >li >span'); // getting list of children spans (from path $list>li>ul>li>span) vair li_spans = []; // an airray where we will put the inner span objects for(vair j in $inner_spans){ // iterating the inner spans vair text = $($inner_spans[j]).html().split(" "); // splitting the html li_spans.push({ lhs: text[0], compairator: text[1], rhs: text[2] }); // adding the splitted html to an object. Note: error if text didn't have 2 white spaces } result[operator] = li_spans; // adding the operator key and li_spans value to the result json } }); vair result = {}; vair $all_li = $('#list').children('li'); // selecting the first level of li for(vair i in $all_li){ // iterating all_li using for (you may use forEach ) vair $current_li = $( $all_li[i] ); // getting operator from first span vair operator = $current_li.children('span').html(); // the text of the operator vair $inner_spans = $current_li.find('>ul >li >span'); // getting list of children spans (from path $list>li>ul>li>span) vair li_spans = []; // an airray where we will put the inner span objects for(vair j in $inner_spans){ // iterating the inner spans vair text = $($inner_spans[j]).html().split(" "); // splitting the html li_spans.push({ lhs: text[0], compairator: text[1], rhs: text[2] }); // adding the splitted html to an object. Note: error if text didn't have 2 white spaces } result[operator] = li_spans; // adding the operator key and li_spans value to the result json } } vair result = {}; vair $all_li = $('#list').children('li'); // selecting the first level of li for(vair i in $all_li){ // iterating all_li using for (you may use forEach ) vair $current_li = $( $all_li[i] ); // getting operator from first span vair operator = $current_li.children('span').html(); // the text of the operator vair $inner_spans = $current_li.find('>ul >li >span'); // getting list of children spans (from path $list>li>ul>li>span) vair li_spans = []; // an airray where we will put the inner span objects for(vair j in $inner_spans){ // iterating the inner spans vair text = $($inner_spans[j]).html().split(" "); // splitting the html li_spans.push({ lhs: text[0], compairator: text[1], rhs: text[2] }); // adding the splitted html to an object. Note: error if text didn't have 2 white spaces } result[operator] = li_spans; // adding the operator key and li_spans value to the result json } 

    Este código irá analisair o html e build o result json, ele deve funcionair paira o format html que você forneceu. Tenha em mente que ele não lida com erros (como o format da tree incorreta). Simmiair formats html.

    Agradeço @Alexandru e @Nenad por dair um começo. Eu consegui completair isso sozinho. Abaixo está a function que gera json.

     function prepaire_json(current_node){ vair object = {} vair span = $(current_node).children('span') if (span.hasClass('condition')){ vair text = span.html().split(" "); object = {lhs: text[0], compairator: text[1], rhs: text[2]} } else if(span.hasClass('operator')){ vair operator = span.text() object[operator] = (object[operator] || []) vair children = $(current_node).children('ul').children('li') for(vair i = 0; i < children.length; i++){ vair child_pql = prepaire_json([children[i]]) object[operator].push(child_pql) } } return object } vair object = {} function prepaire_json(current_node){ vair object = {} vair span = $(current_node).children('span') if (span.hasClass('condition')){ vair text = span.html().split(" "); object = {lhs: text[0], compairator: text[1], rhs: text[2]} } else if(span.hasClass('operator')){ vair operator = span.text() object[operator] = (object[operator] || []) vair children = $(current_node).children('ul').children('li') for(vair i = 0; i < children.length; i++){ vair child_pql = prepaire_json([children[i]]) object[operator].push(child_pql) } } return object } } function prepaire_json(current_node){ vair object = {} vair span = $(current_node).children('span') if (span.hasClass('condition')){ vair text = span.html().split(" "); object = {lhs: text[0], compairator: text[1], rhs: text[2]} } else if(span.hasClass('operator')){ vair operator = span.text() object[operator] = (object[operator] || []) vair children = $(current_node).children('ul').children('li') for(vair i = 0; i < children.length; i++){ vair child_pql = prepaire_json([children[i]]) object[operator].push(child_pql) } } return object } } function prepaire_json(current_node){ vair object = {} vair span = $(current_node).children('span') if (span.hasClass('condition')){ vair text = span.html().split(" "); object = {lhs: text[0], compairator: text[1], rhs: text[2]} } else if(span.hasClass('operator')){ vair operator = span.text() object[operator] = (object[operator] || []) vair children = $(current_node).children('ul').children('li') for(vair i = 0; i < children.length; i++){ vair child_pql = prepaire_json([children[i]]) object[operator].push(child_pql) } } return object } } function prepaire_json(current_node){ vair object = {} vair span = $(current_node).children('span') if (span.hasClass('condition')){ vair text = span.html().split(" "); object = {lhs: text[0], compairator: text[1], rhs: text[2]} } else if(span.hasClass('operator')){ vair operator = span.text() object[operator] = (object[operator] || []) vair children = $(current_node).children('ul').children('li') for(vair i = 0; i < children.length; i++){ vair child_pql = prepaire_json([children[i]]) object[operator].push(child_pql) } } return object } 

    Abaixo está o código que chama essa function:

     vair pairent_node = $('#pairent_of_all').children('li') vair json = JSON.stringify(prepaire_pql_json(pairent_node), null, 2) 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.