> How would I use jQuery to write the values onto a page?  And how do
> you go the other way, taking values and serializing them into JSON to
> be sent to the server?

For serializing, on the javascript side, between objects and JSON, you
can use this public domain library: http://json.org/json2.js.  I
*think* it's Douglas Crockford's code, but I'm not sure.

If you're using .NET on the server, you can use the following to
serialize to/from your .NET classes:

   using System.IO;
   using System.Runtime.Serialization.Json;
   using System.Text;

   public class JsonApi
   {
      static public string ObjectToJson<T>(T obj)
      {
         string json = null;
         DataContractJsonSerializer s = new
DataContractJsonSerializer(typeof(T));
         using (MemoryStream ms = new MemoryStream())
         {
            s.WriteObject(ms, obj);
            json = Encoding.Default.GetString(ms.ToArray());
         }
         return json;
      }
      static public T JsonToObject<T>(string json) where T : class
      {
         T obj = null;
         using (MemoryStream ms = new
MemoryStream(Encoding.Unicode.GetBytes(json)))
         {
            DataContractJsonSerializer s = new
DataContractJsonSerializer(typeof(T));
            obj = s.ReadObject(ms) as T;
         }
         return obj;
      }
   }

For a class to work with the above code, it needs attributes added
like this:

   [Serializable] [DataContract]
   public class jLaunchItem
   {
      public jLaunchItem(string id, string label)
      {
         this.id = id;
         this.label = label;
      }
      [DataMember] public string id { get; set; }
      [DataMember] public string label { get; set; }
   }

Reply via email to