Reputation: 179
I have a jquery third party application that has nested lists that serialize to an output like below. The list will always only have 2 levels, but I am having trouble trying to figure out how to parse it. I am using coldFusion.
The List Looks like (line breaks added for visualization, they cannot be used as a delimiter):
[{"id":1},
{"id":197,"children":[{"id":198},{"id":199},{"id":200}]},
{"id":2,"children":[{"id":3},{"id":4},{"id":143},{"id":6},{"id":5},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12}]},
{"id":15,"children":[{"id":17},{"id":190},{"id":191},{"id":131},{"id":16},{"id":142},{"id":124}]},
{"id":114}]
I want to loop through each id and convert into a parentid and childid like so:
id:1 parentid: 10000 childid: 10000
id:197 parentid: 10001 childid: 10000 (new parent)
id:198 parentid: 10001 childid: 10001 (first child)
id:199 parentid: 10001 childid: 10002 (second child)
id:200 parentid: 10001 childid: 10003 (third child)
id:2 parentid: 10002 childid: 10000 (new parent)
... and so on
Your help is appreciated.
Edit: Code is below for what I am trying to do
<script type="text/javascript">
$(document).ready(
function()
{
var updateOutput = function(e)
{
var list = e.length ? e : $(e.target),
output = list.data('output');
if (window.JSON) {
output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
} else {
output.val('JSON browser support required for this demo.');
}
};
//this is where i need help
var postOutline = function(output){
$.post("something.cfc", {
method: 'getoutline',
output: output
});
};
// activate Nestable for list 1
$('#nestable3').nestable({
group: 1
})
// .on('change', updateOutput);
.on('change', postOutline);
// output initial serialised data
updateOutput($('#nestable3').data('output', $('#nestable-output')));
}
);
</script>
Upvotes: 0
Views: 1743
Reputation: 29870
You just need to use deserializeJson()
. You don't need to parse it by hand. From the docs:
Description Converts a JSON (JavaScript Object Notation) string data representation into CFML data, such as a CFML structure or array.
From there you simply need to use your usual CFML to process it however you like.
Finding the children for each ID is easy as it's in the data structure.
Unfortunately the data structure is not ideal for extracting parent information.
The most expedient way I can think of is to use structFindValue() to find all the occurrences of the current ID, and then loop over that finding the entry for which the match has a descendant children". Then traverse across to the ID, which will be the parent ID of those children (if that makes sense).
(the above initial suggestion won't work, as structFindValue() doesn't give enough information).
You're gonna need to brute-force this, doing something like this:
array = deserializeJson(json); // json is your data from the client
for (childElement in array){
childId = childElement.id;
for (parentElement in array){
if (structKeyExists(parentElement, "children")){ // not all of them have children
if (arrayFind(parentElement.children, {id=childId})){
writeOutput("Parent of #childId# is #parentElement.id#<br>");
}
}
}
}
Obviously that's not a precise solution for what you need, but it shows the technique for looking-up the parent. Someone else might be able to come up with a less ham-fisted way of doing it.
Upvotes: 1