DataContractJsonSerializer with JavaScriptSerializer.deserialize()

Discussion in 'ASP .Net' started by Ken Adeniji, Feb 28, 2008.

  1. Ken Adeniji

    Ken Adeniji Guest

    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;
    }
     
    Ken Adeniji, Feb 28, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Assaf
    Replies:
    1
    Views:
    513
    Justin Rogers
    Feb 7, 2004
  2. Sidney Pecul

    How to "deserialize" session contents??

    Sidney Pecul, May 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    942
    William F. Robertson, Jr.
    May 8, 2004
  3. Replies:
    3
    Views:
    649
  4. j0hn_P
    Replies:
    0
    Views:
    447
    j0hn_P
    Apr 8, 2004
  5. Dave Sanders
    Replies:
    2
    Views:
    1,253
    Dave Sanders
    Mar 25, 2008
Loading...

Share This Page