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;
}
 
Ad

Advertisements


Top