0

我正在尝试反序列化从基于此代码的.NET 3.5 Winforms 应用程序调用的 Web API 方法收到的一些 json:http: //msdn.microsoft.com/en-us/library/bb412179 (v=vs.90 ).aspx

正在返回 json,但在反序列化时,它无法获取根并因此咆哮:

在此处输入图像描述

这是有问题的客户端代码:

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);
}

response.Content 行运行良好,在对话框中显示 json 数据。

部门数据在 .NET 4 ASP.NET / Web API 应用程序中以这种方式定义:

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

...在接收数据的 .NET 3.5 Winforms 应用程序中以这种方式:

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

那么它还需要做什么呢?我如何为其提供“根”元素,因为它似乎要求很高?

更新

Badri 的回答解决了错误消息,但我仍然没有得到任何数据来使用 DataContractJsonSerializer,或者我访问错误。这是我现在的代码:

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));

...而且,虽然第一个消息框显示了 jsonarray:

在此处输入图像描述

...第二个说 accountId 和 deptName 是空字符串。我以什么方式滥用 DataContractJsonSerializer?

4

1 回答 1

1

deptStream是新的,但是在反序列化之前,你在哪里加载返回到MemoryStream, 的 JSON 响应。你应该做这样的事情。

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

// Deserialize now

更新 您的 JSON 对应于Department对象列表而不是单个Department对象。尝试这样的事情。

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));
于 2013-10-15T01:28:56.513 回答