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

时间:2013-12-11 00:16:09

标签: c# json serialization json.net

如果我理解UfukHacıoğullarıhere

我可以简化这段代码:

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,而在客户端上这是一个清单:

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

那么我怎样才能更直接地将JSON对象传递给我的客户端,而不是使用JArray / JObject?这真的可行吗?

1 个答案:

答案 0 :(得分:1)

这就是我最终的结果; 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