Json反序列化

zlp321002 2016-11-23 01:38:49
string result = "{\"image_id\": \"LZqkfgZdaRVzQvwqnCWwCQ==\", \"request_id\": \"1479872923803e24e2-2f93-4e02-86ef-94321d74d575\",\"time_used\": 310,\"\faces\":[{\"attributes\":{\"gender\": {\"value\": \"Female\"},\"age\": {\"value\": 20}},\"\face_rectangle\":{\"width\":180,\"top\":91,\"left\":272,\"height\":181},\"face_token\":\"ff48424df99df8543b3c89781483101f\"}]}";


myface datas = JsonConvert.DeserializeObject<myface>(result);

foreach (faces faceslist in datas.faces)
{
string ages = faceslist.attributes.age.value;
}

后面的值,为什么解析不到呢?
...全文
724 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lutaotony 2016-11-23
  • 打赏
  • 举报
回复
2楼描述应该正确,你应该先看看你的json串是否正确。 然后才开始做解析的步骤。http://tool.oschina.net/codeformat/json http://blog.csdn.net/lutaotony/article/details/50460836 不知道是不是你想要的
zlp321002 2016-11-23
  • 打赏
  • 举报
回复
   public class myface
        {
            public string image_id { get; set; }
            public string request_id { get; set; }
            public string time_used { get; set; }
            public faces[] faces { get; set; }
        }

        public class faces
        {
            public Attributes attributes { get; set; }
            public string face_token { get; set; }
        }

        public class Attributes
        {
            public gender gender { get; set; }
            public age age { get; set; }
            public glass glass { get; set; }
            public headpost headpost { get; set; }
            public smile smail { get; set; }
        }
        public class gender
        {
            public string value { set; get; }
        }

        public class age
        {
            public string value { set; get; }
        }


        public class glass
        {
            public string value { set; get; }
        }

        public class headpost
        {
            public string value { set; get; }
        }
        public class smile
        {
            public string threshold { set; get; }
            public string value { set; get; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            string result = "{\"image_id\": \"LZqkfgZdaRVzQvwqnCWwCQ==\", \"request_id\": \"1479872923803e24e2-2f93-4e02-86ef-94321d74d575\",\"time_used\": 310,\"\faces\":[{\"attributes\":{\"gender\": {\"value\": \"Female\"},\"age\": {\"value\": 20}},\"\face_rectangle\":{\"width\":180,\"top\":91,\"left\":272,\"height\":181},\"face_token\":\"ff48424df99df8543b3c89781483101f\"}]}";


            myface datas = JsonConvert.DeserializeObject<myface>(result);

            foreach (faces faceslist in datas.faces)
            {
                string ages = faceslist.attributes.age.value;
            }
                
         
        }
我这么解析的,不知道为什么解析不了?楼上的二位!
巴士上的邂逅 2016-11-23
  • 打赏
  • 举报
回复
你的字符串应该有问题,注意标红处 string result = "{\"image_id\": \"LZqkfgZdaRVzQvwqnCWwCQ==\", \"request_id\": \"1479872923803e24e2-2f93-4e02-86ef-94321d74d575\",\"time_used\": 310,\"\faces\":[{\"attributes\":{\"gender\": {\"value\": \"Female\"},\"age\": {\"value\": 20}},\"\face_rectangle\":{\"width\":180,\"top\":91,\"left\":272,\"height\":181},\"face_token\":\"ff48424df99df8543b3c89781483101f\"}]}";
正怒月神 2016-11-23
  • 打赏
  • 举报
回复
vs 编辑 选择性黏贴 你的 json 看一下生成的Model类就可以了

13,347

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET技术前瞻
社区管理员
  • .NET技术前瞻社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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