I'm trying to use Knockout's mapping plugin on a nested JSON object with variable data inside. However, I'm not sure how to get it to display in my HTML. How do I correctly map all the nested JSON objects and display it as, say, a simple string? Here is my code:
JS
var ListModel = function(jsonData) {
var self = this;
self.master = ko.mapping.fromJS(jsonData);
}
var listModel = new ListModel(jsonData);
ko.applyBindings(listModel);
HTML
<!-- ko foreach: master -->
<div data-bind="text: $data"></div>
<!-- /ko -->
Sample JSON
{"Level 1a":"Hi","Level 1b":{
"Level 2a":"Hello","Level 2b":{
"Level 3":"Bye"}
}
}
Sample Output
Hi
Hello
Bye
The main thing I'm trying to do here is to print out the values from all nested levels. The key values and number of nested levels are entirely variable (most of the nested JSON examples I found on SO and online were for fixed keys). Is this possible?
Update: I found the jQuery equivalent, but I still need the Knockout implementation for observables.