I have a webservice that outputs a JSON string upon request.
Currently, I have a class set up to receive that looks like this
[DataContract]
public class user_new_register
{
[DataMember(Name="ErrorStatus")]
public string ErrorStatus{ get; set; }
[DataMember(Name="ErrorMessages")]
public string []ErrorMessages { get; set; }
[DataMember(Name="UserID")]
public int UserID { get; set; }
}
I pass the string to the server like this
var httpReq = (HttpWebRequest)HttpWebRequest.Create (new Uri (createUser));
httpReq.BeginGetResponse ((ar) => {
var request = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse (ar)) {
var s = response.GetResponseStream ();
This works and “s” contains the response from the server ok. My problem is now how to deserialize the reply to the [DataContract] class.
I have tried the following, but it doesn’t work
user_new_register register = new user_new_register ();
var stream = new MemoryStream ();
var serializer = new DataContractJsonSerializer (typeof(json.user_new_register));
var httpReq = (HttpWebRequest)HttpWebRequest.Create (new Uri (createUser));
httpReq.BeginGetResponse ((ar) => {
var request = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse (ar)) {
var s = response.GetResponseStream ();
stream = (JsonObject)JsonObject.Load (s);
register = (user_new_register)serializer.ReadObject (s);
This compiles but crashes at the register = line
Any help or advice would be appreciated here 🙂
Thanks
Paul
I would recommend using json.net. There is a branched version for Mono that is about a year back (version 4.0), but I have used it on more than a handful of projects and it has been reliable.