I’m using JavascriptSerializer with a custom JavascriptConverter to serialize two objects and return an array of these objects. I’m creating the array using Stringbuilder somewhat like this:
JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
TheSerializer.RegisterConverters(new JavaScriptConverter[] { new MyConverter()});
TheStringBuilder.Append("[");
TheStringBuilder.Append(TheSerializer.Serialize(FirstObject));
TheStringBuilder.Append("],[");
TheStringBuilder.Append(TheSerializer.Serialize(SecondObject));
TheStringBuilder.Append("]");
TheJsonToClient = TheStringBuilder.ToString();
The serialization and the custom converter work fine because in other parts of the code I’m using those and on the client JSON.parse work well for these objects.
The problem is that when I’m reading the return string and calling JSON.parse on it, the deserialization fails on the client with an Unexpected Token error during the parsing.
What am I doing wrong with the encoding that could cause this error when I’m deserializing the arrray?
Thanks for your suggestions.
This line:
Should read:
Valid JSON would be something like
[1,2]— your code is generating[1],[2], which is invalid JSON.Also, have you considered trying this instead of building the JSON array by hand?