I have a Javascript object like this:
var jsonDataActual = {
"source": [{
"name": "testCaption0",
"desc": "testDescrip0",
"values": [{
"from": "/Date(1338811241074)/",
"to": "/Date(1346760041074)/",
"label": "testLabel0",
"customClass": "GanttRed"
}]
}],
"navigate": "scroll",
"scale": "weeks",
"maxScale": "months",
"minScale": "days",
"itemsPerPage": 11,
"onItemClick": function (data) { },
"onAddClick": function (dt, rowId) { }
};
it works fine for me. Now, because of my requirement and need, am making this entire object(including braces i.e.{ } and semicolon 😉 as a string on server side (C#) and returning it to an ajax call (web Method) using a server side method:
in the server side method I do something like this:
return new JavaScriptSerializer().Serialize(jsonData);
but now this entire returned data (inside Success: function(msg){ var s=msg.d} ) is treated as string, hence it doesn’t work.
I tried these:
1. var obj = eval('{' + msg.d +'}'); (removing beginning and ending braces in msg.d)
typeof(obj) is string. failed
2. eval('var obj ='+msg.d); (including beginning and ending braces in msg.d)
typeof(obj) is string. failed
3. var obj= jQuery.parseJson(msg.d);
typeof(obj) is string. failed
4. var obj = new Object();
//var obj = {};
obj=jQuery.parseJson(msg.d).
typeof(obj) is string. failed.
please help. How can I convert a server side returned json into object?
Why not is it working for me??. Is it because of structure of my json object??
and why does jsonDataActual works for me but not when sent as a string???
Please Help…..
found out solution to my specific problem.
I was constructing a string variable with my json data as its value. And then I would return this string to the client side function(the one making ajax request). i.e.
Server side method
but it was not working. So instead of constructing a string variable and serializing it, I wrote a class with specific structure and sent it in the return statement(after serialzing).
i.e. see below classes
and I would use this class as below:
wrapping up my entire data into a class structure and then serializing this worked for me. my client side function successfully recognized it as an object i.e.