Question

I am trying to deserialize some json received from a Web API method being called from a .NET 3.5 Winforms app based on this code: http://msdn.microsoft.com/en-us/library/bb412179(v=vs.90).aspx

The json is being returned, but upon deserialization, it is failing to grab root and therefore growling:

enter image description here

Here is the client code in question:

try
{
    var client = new RestClient();
    client.BaseUrl = "http://localhost:48614/"; // <-- this works
    var request = new RestRequest();
    request.Resource = "api/departments/"; // can replace this with other data, such as redemptions, etc.
    RestResponse response = client.Execute(request) as RestResponse;
    if ((response.StatusCode == HttpStatusCode.OK) && (response.ResponseStatus == ResponseStatus.Completed)) // Both are probably not necessary
    {
        MessageBox.Show(string.Format("Content is {0}", response.Content));
        // from http://msdn.microsoft.com/en-us/library/bb412179(v=vs.90).aspx
        MemoryStream deptStream = new MemoryStream();
        DataContractJsonSerializer cereal = new DataContractJsonSerializer(typeof(Department));
        deptStream.Position = 0;
        Department dept = (Department)cereal.ReadObject(deptStream);
        MessageBox.Show(string.Format("accountId is {0}, deptName is {1}", dept.AccountId, dept.DeptName));
    }
    else
    {
        MessageBox.Show(string.Format("Status code is {0} ({1}); response status is {2}",
            response.StatusCode, response.StatusDescription, response.ResponseStatus));
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

The response.Content line is doing just fine, displaying the json data in the dialog.

The Department data is defined this way in the .NET 4 ASP.NET / Web API app:

namespace DuckbilledPlatypusServerWebAPI.Models
{
    public class Department
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string AccountId { get; set; }
        [Required] 
        public string DeptName { get; set; }
    }
}

...and this way in the .NET 3.5 Winforms app that receives the data:

[DataContract]
public class Department
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string AccountId { get; set; }
    [DataMember] 
    public string DeptName { get; set; }
}

So what does it yet need to work? How am I to supply it with a 'root' element, as it seems to be demanding?

UPDATE

Badri's answer solves the err msg, but I'm still not getting any data to work with the DataContractJsonSerializer, or I'm accessing it wrong. Here is my code now:

MessageBox.Show(string.Format("Content is {0}", response.Content));
byte[] bytes = Encoding.UTF8.GetBytes(response.Content);
MemoryStream deptStream = new MemoryStream(bytes);
deptStream.Position = 0;
DataContractJsonSerializer jasonCereal = new DataContractJsonSerializer(typeof(Department));
Department dept = (Department)jasonCereal.ReadObject(deptStream);
MessageBox.Show(string.Format("accountId is {0}, deptName is {1}", dept.AccountId, dept.DeptName));

...and, although the first message box shows the jsonarray:

enter image description here

...the second one says accountId and deptName are empty strings. In what way am I maltrreating DataContractJsonSerializer?

Was it helpful?

Solution

deptStream is newed up but where do you load the JSON response returned into the MemoryStream, before the deserialization. You should do something like this.

byte[] bytes = Encoding.UTF8.GetBytes(response.Content);
MemoryStream deptStream = new MemoryStream(bytes);
deptStream.Position = 0;

// Deserialize now

UPDATE Your JSON corresponds to a list of Department objects not a single Department object. Try something like this.

var jasonCereal = new DataContractJsonSerializer(typeof(List<Department>));
var depts = (List<Department>)jasonCereal.ReadObject(deptStream);
foreach(var dept in depts)
    MessageBox.Show(
         String.Format("accountId is {0}, deptName is {1}",
                                        dept.AccountId, dept.DeptName));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top