I have an object model called MyObject that I’m serializing to a json string and I’m storing that string in the DB. I want to send several of these objects to the page.
When the objects come out of the DB, they’re put into a list of objects called JsonCache like this:
public class JsonCache
{
public int ObjectID {get;set;}
public byte ObjectStatus {get;set;}
public string MyObjectInJsonFormat {get;set;}
}
List<JsonCache> MyListOfMyObjects = new List<JsonCache>();
MyObjectInJsonFormat is the string that contains all the properties of MyObject (9 lists of nested objects.)
Before sending this list to the page, I serialize it with a simple serializer like this:
JavaScriptSerializer ObjectSerializer = new JavaScriptSerializer();
string LeadsToPage = ObjectSerializer.Serialize(MyListOfObjects);
and then I put this string in an aspx literal that’s inside a div called MyDiv.
In the client page, I write this javascript:
var ObjectsFromServer = $('#MyDiv').html();
ObjectsInJson = eval(ObjectsFromServer);
The problem is that ObjectsInJson is an array that contains the JsonCache object model, with the string ObjectInJsonFormat. Of course, I could de a recursive eval but that’s not what I want. What I’m looking to do is have ObjectInJson be an array of MyObject.
What do I need to change to make this work?
Thanks for your suggestions.
You could create a custom serializer like JavaScriptConverter for JsonCache and do something special for MyObjectInJsonFormat.