1

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.

1
  • JavaScript string can't have double quotes inside double quotes. You need either escape them (\") or use single quotes (') inside/outside. Commented Oct 12, 2013 at 17:06

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I did what you told me to, but i´m still getting the "c()" when I print "mappedBanks.Items"
actually it works but I changed it for this "var mappedBanks = { items: JSON.parse(data.d) };" ..
Your code is the same excepting that items will be plain array, not observableArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.