On the server side I have a Asp.Net Web application, a WebMethod returns a Json string serialized just like this one:
Object { d= "[{"Id":"1","Name":"COMERCIAL BANK"},
{"Id":"2","Name":"AZTEC BANK"},
{"Id":"3","Name":"EL SALVADOR BANK"}]" }
When I try mapping that result using var mappedBanks = ko.mapping.fromJSON(data.d)
, and then use console.log(mappedBanks)
all I get printed is c()
and is like that mappedBanks, that should be an array, has no elements because I can iterate on it and when I try to print the first element, the console says undefined
. Is there a problem with the Json? or I´m not mapping it right.
ko.mapping.toJSON
requires first argument to be an object, not an array. Your option is to make your JSON-encoded array to be property value.
var mappedBanks = ko.mapping.fromJS({ items: JSON.parse(data.d) });
console.log(mappedBanks.items);