为什么我的JSON响应没有在我的客户端上反序列化为与Web API服务器上的原始IEnumerable类型相同的通用列表



如果我理解Ufuk Hacıoğulları这里的

我可以简化这个代码:

using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    if (webResponse.StatusCode == HttpStatusCode.OK)
    {
        var reader = new StreamReader(webResponse.GetResponseStream());
        string s = reader.ReadToEnd();
        var arr = JsonConvert.DeserializeObject<JArray>(s);
        if (arr.Count <= 0) break;
        foreach (JObject obj in arr)
        {
            id = obj.Value<int?>("Id") ?? 0;
            var _deptId = obj.Value<int?>("deptId") ?? 0;
            var _subdeptId = obj.Value<int?>("subdeptId") ?? 0;
            var _deptIdSubdeptId = Convert.ToDouble(String.Format("{0}.{1}", _deptId, _subdeptId));
            var _subdeptName = obj.Value<string>("subdeptName") ?? "";
            subDeptList.subDepartments.Add(new HHSUtils.Subdepartment
            {
                Id = id,
                deptIdSubdeptId = (float)_deptIdSubdeptId,
                subDeptName = _subdeptName
            });
        } // foreach
    } // if ((webResponse.StatusCode == HttpStatusCode.OK)
} // using HttpWebResponse

如果我要"使用自定义类型而不是JArray或JObject类型"

如果我理解他的话,我可以替换这句话:

var arr = JsonConvert.DeserializeObject<JArray>(s);

比如:

var subDeptList = new SubdeptsList();
. . .
var arr = JsonConvert.DeserializeObject<Subdepartment>(s);
foreach (HHSUtils.Subdepartment obj in arr)
{
    subDeptList.subDepartments.Add(obj);
} 

然而,这样做失败了,"异常:无法将JSON数组反序列化为类型'HHS.HHSUtils+Subdepartment'。"

服务器通过的是一个IEnumerable,它在那里被定义为:

public class Redemption
{
    [Key]
    public long Id { get; set; }
    [Required]
    public string RedemptionId { get; set; }
    [Required]
    public string RedemptionName { get; set; }
    [Required]
    public string RedemptionItemId { get; set; }
    [Required]
    public double RedemptionAmount { get; set; }
    public string RedemptionDept { get; set; }
    public string RedemptionSubDept { get; set; }
}

它在将数据传回时将其转换为JSON;在客户端中,我试图将发送的JSON转换回以下内容:

public class Redemption
{
    public long Id { get; set; }
    public string RedemptionId { get; set; }
    public string RedemptionItemId { get; set; }
    public string RedemptionName { get; set; }
    public double RedemptionAmount { get; set; }
    public string RedemptionDept { get; set; }
    public string RedemptionSubDept { get; set; }
}

使用上面显示的代码。我能看到的唯一不匹配(除了Redemption在服务器上的类成员"装饰"(Key,Required),而在客户端上却没有)是,在服务器上,通用列表是IEnumerable,而在客户机上,它是list:

public class SubdeptsList
{
    public List<Subdepartment> subDepartments = new List<Subdepartment>();
}

那么,如何更直接地将JSON对象传输到我的客户端,而不是使用JArray/JObject呢?这真的可行吗?

这就是我最终得到的结果;Ufuk的建议带来了更加精简/优雅的代码:

// the old way:
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    if (webResponse.StatusCode == HttpStatusCode.OK)
    {
        var reader = new StreamReader(webResponse.GetResponseStream());
        string s = reader.ReadToEnd();
        var arr = JsonConvert.DeserializeObject<JArray>(s);
        if (arr.Count <= 0) break; 
        foreach (JObject obj in arr)
        {
            id = obj.Value<int?>("Id") ?? 0;
            var _redemptionId = obj.Value<string>("RedemptionId") ?? "";
            var _redemptionItemId = obj.Value<string>("RedemptionItemId") ?? "";
            var _redemptionName = obj.Value<string>("RedemptionName") ?? "";
            double _redemptionAmount = obj.Value<double?>("RedemptionAmount") ?? 0.0;
            var _redemptionDept = obj.Value<string>("RedemptionDept") ?? "";
            var _redemptionSubdept = obj.Value<string>("RedemptionSubDept") ?? "";
            redemptionsList.redemptions.Add(new Redemption
            {
                Id = id,
                RedemptionId = _redemptionId,
                RedemptionItemId = _redemptionItemId,
                RedemptionName = _redemptionName,
                RedemptionAmount = _redemptionAmount,
                RedemptionDept = _redemptionDept,
                RedemptionSubDept = _redemptionSubdept
            });
        } // foreach
    } // if ((webResponse.StatusCode == HttpStatusCode.OK)
} // using HttpWebResponse

现在,新的和改进的方式:

using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    if (webResponse.StatusCode == HttpStatusCode.OK)
    {
        var reader = new StreamReader(webResponse.GetResponseStream());
        string jsonizedRedemptions = reader.ReadToEnd();
        List<Redemption> redemptions = JsonConvert.DeserializeObject<List<Redemption>>(jsonizedRedemptions);
        if (redemptions.Count <= 0) break;
        foreach (Redemption redemption in redemptions)
        {
            redemptionsList.redemptions.Add(redemption);
        }
    } // if ((webResponse.StatusCode == HttpStatusCode.OK)
} // using HttpWebResponse

相关内容

  • 没有找到相关文章

最新更新