Json反序列化对象求解

oreoconansisu 2014-07-07 10:43:55
引用Newtonsoft.Json.dll 版本号4.5.0.0


主要代码如下:
static void Main(string[] args)
{
string json = "{\"response\":{\"errorCount\":2,\"totalCount\":2}}";

OrderResponse res = JsonConvert.DeserializeObject<OrderResponse>(json);

Console.WriteLine("ErrCount:{0}", res.ErrCount);
Console.WriteLine("TotalCount:{0}", res.TotalCount);
}


[DataContract(Name = "response")]
public class OrderResponse
{
[DataMember(Name = "errorCount")]
public int ErrCount { get; set; }

[DataMember(Name = "totalCount")]
public int TotalCount { get; set; }
}


反序列化后ErrCount,TotalCount属性没有取到,求解

先谢谢了
...全文
150 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangnaisheng 2014-07-07
  • 打赏
  • 举报
回复
JSON 序列化和反序列化 类似你的,参考
leozdgao 2014-07-07
  • 打赏
  • 举报
回复
string json = "{\"errorCount\":2,\"totalCount\":2}";
  • 打赏
  • 举报
回复
你直接new一个OrderResponse对象出来,然后序列化成json,看和你的json字符串是不是一样
宝_爸 2014-07-07
  • 打赏
  • 举报
回复

   public class OrderResponseWrapper
    {
        public OrderResponse response { get; set; }
    }

    [DataContract(Name = "response")]
    public class OrderResponse
    {
        [DataMember(Name = "errorCount")]
        public int ErrCount { get; set; }

        [DataMember(Name = "totalCount")]
        public int TotalCount { get; set; }
    }

            string json = "{\"response\":{\"errorCount\":2,\"totalCount\":2}}";
            
            OrderResponseWrapper res = JsonConvert.DeserializeObject<OrderResponseWrapper>(json);

            Console.WriteLine("ErrCount:{0}", res.response.ErrCount);
            Console.WriteLine("TotalCount:{0}", res.response.TotalCount);


111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧