I need to map a JSON string received from a server to a specific Javascript object -


all,

i have following code:

$.ajax({     url: '@url.action("getmerchantusers", "merchant")',     datatype: 'json',     success: function (json) {         var mappedtasks = $.map(json.parse(json), function (item) { return new task(item) });         self.tasks(mappedtasks);     } }); 

this calls mvc controller returns list of objects jsonresult method. works totally fine. however, need rewrite method because there never more 1 task being returned server. when return 1 task server, however, .net jsonresult method doesn't put '[' , ']' @ beginning , end of json, $.map() sees properties of object collection, want map 1 object returned server task observable instead of multiple tasks tasks observable. i'm new knockout...how map single json object, i'm doing above collection. i'll happy provide more info if needed!

also, i've mapped object generic javascript type, want map task type specifically.

since you're no longer processing list, don't need $.map() (since loops through , applies function every item in list), can pass parsed json reponse task constructor...like perhaps:

var mappedtasks = new task(json.parse(json)); 

if self.tasks() method expects list, can wrap it:

self.tasks([mappedtasks]); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -