DataContractJsonSerializer with JavaScriptSerializer.deserialize()

K

Ken Adeniji

I am attempting to deserialize the Json response below:
"{"customerNumber":19,"giftMessage":"Good."}"

//Javascript
var shipToInfo = Sys.Serialization.JavaScriptSerializer.deserialize(result);

//.NET Framework 3.5 it fails.
public static string ToJson<T>(this T obj)
{
MemoryStream stream = new MemoryStream();

try
{
//serialize data to a stream, then to a JSON string
/* The DataContractJsonSerializer requires using
System.Runtime.Serialization.Json;
* which is in the System.ServiceModel.Web assembly.
*/
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(T));
serializer.WriteObject(stream, obj);

return Encoding.UTF8.GetString(stream.ToArray());
}
finally
{
stream.Close();
stream.Dispose();
}
}

//.NET Framework 2.0 it works.
public static string ToJson(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}

A Runtime Error has occured. Do you wish to Debug? Line 604 Error:
Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in
a correct format. Yes No.

if (error && !errorHandled) {
// DevDiv 89485: throw, don't alert()
throw error;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top