This project has moved. For the latest updates, please go here.

Serialize a JSon Object into a Stream

Jun 30, 2008 at 4:00 PM

I would like to post a JSON Object to an HTTP server. I already wrote (hardly) this piece of code :

JObject rq = new JObject(
  new JProperty("controller","database"),
  new JProperty("action","info"),
  new JProperty("connect",
    new JObject(
      new JProperty("databasename", "test"),
      new JProperty("user", "dba"),
      new JProperty("password", "password"),
      new JProperty("dialect", "3")

HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8132");
HttpWRequest.Credentials = new NetworkCredential("user","pass");
HttpWRequest.Method = WebRequestMethods.Http.Post;
HttpWRequest.ContentType = "application/json";
HttpWRequest.Accept = "application/json";
Stream PostData = HttpWRequest.GetRequestStream();

Now, how to serialize (as a string), the JSonObject into the Stream ?

Many thanks for your help.


Pierre Y.