Ich verwende jQuery, um verschachtelte Listen als JSON auszugeben.Verschachtelte Listen als strukturierte JSON schreiben
Ich habe Probleme mit den verschachtelten Elementen. Ich habe ein Plunk erstellt, mit dem Sie das Problem mit einem einfachen Klick auf eine Schaltfläche neu erstellen können.
Plunk: https://plnkr.co/edit/jLi9epblNAtMbzezcRSY?p=preview
HTML:
<h1>Hello Plunker!</h1>
<div class="dd" name="agenda-nestable" id="nestable">
<ol id="agenda-root" class="dd-list">
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Pledge of Allegiance</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Roll Call</div>
<ol class="dd-list">
<li class="dd-item" data-id="0">
<div class="dd-handle">Establish a Quorum</div></li>
</ol>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Public Comment</div>
<ol class="dd-list">
<li class="dd-item" data-id="0">
<div class="dd-handle">Address</div></li>
<li class="dd-item" data-id="0">
<div class="dd-handle">Open Floor</div></li>
</ol>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Action to set agenda and to approve consent agenda items</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Presentations and awards</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Matters set for a specific time</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Regular Agenda</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Governing Board</div>
</li>
<li class="dd-item" data-id="0" id="0">
<div class="dd-handle">Closed Session</div>
</li>
</ol>
</div>
<pre id="jsonOutput"></pre>
<button value onclick='convertToJson()'>Convert nodes to JSON</button>
Code:
function convertToJson() {
var sectionOrder = "";
var itemOrder = "";
var sectionDelimiter = "";
var itemDelimiter = "";
var agendaDiv = $("[name='agenda-nestable']");
var agendaSections = $(agendaDiv).find("ol#agenda-root>li");
var sections = [];
for (var i = 0; i < agendaSections.length; i++) {
var agendaSection = agendaSections[i];
var section = {};
section.Id = $(agendaSection).attr('data-id');
section.SectionText = $(agendaSection).find("div:first-child").text(); // Something wrong here
section.Items = [];
var sectionItems = $(section).find("ol>li");
for (var j = 0; j < sectionItems.length; j++) {
var sectionItem = sectionItems[j];
var item = {};
item.Id = $(sectionItem).attr('data-id');
item.ItemText = $(sectionItem).find("div:first-child").text(); // Something wrong here
section.Items.push(item);
}
sections.push(section);
}
var json = JSON.stringify(sections, null, 2);;
$('#jsonOutput').text(json);
console.log(json);
return json;
}
Ausgang:
{
"Id": "0",
"SectionText": "Pledge of Allegiance",
"Items": []
},
{
"Id": "0",
"SectionText": "Roll CallEstablish a Quorum", // THIS IS WRONG, should be in Items Array, not munged together
"Items": []
},
{
"Id": "0",
"SectionText": "Public CommentAddressOpen Floor", // THIS IS WRONG, should be in Items Array, not munged together
"Items": []
},
{
"Id": "0",
"SectionText": "Action to set agenda and to approve consent agenda items",
"Items": []
},
{
"Id": "0",
"SectionText": "Presentations and awards",
"Items": []
},
{
"Id": "0",
"SectionText": "Matters set for a specific time",
"Items": []
},
{
"Id": "0",
"SectionText": "Regular Agenda",
"Items": []
},
{
"Id": "0",
"SectionText": "Governing Board",
"Items": []
},
{
"Id": "0",
"SectionText": "Closed Session",
"Items": []
}
]
Vielen Dank für einen Blick, ich schätze es wirklich !!!
Philip
"Ich habe Probleme". Was sind die Probleme? Der Code im Link scheint nicht der gleiche zu sein wie in der Frage – ADyson