Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataContractJsonSerializer with JavaScriptSerializer.deserialize()

Reply
Thread Tools

DataContractJsonSerializer with JavaScriptSerializer.deserialize()

 
 
Ken Adeniji
Guest
Posts: n/a
 
      02-28-2008
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.PageRequestManagerServerErrorExceptio n: Input string was not in
a correct format. Yes No.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataContractJsonSerializer and inherited objects Dave Sanders ASP .Net 2 03-25-2008 01:17 PM



Advertisments