在.net中怎么解析json串 [Error reading JObject from JsonReader. Current JsonReader item is not an obj]

编辑时间:2017-05-10,增加一种转化list的方法

一、以前知道一种解析json串的方法,觉得有点麻烦。就从别的地方搜到了另一种

string json = vlt.getlist();

JObject jo = JObject.Parse(json);

var data = jo.getValue("data").ToObject<T>();

T就是对应的实体类,就可以直接data.成员变量使用了

二、json转化list是为了放到redis缓存里面,然后取出进行转化

废话不说,上代码:

            var t = new List<PcWareListByCourseId>();
            var m1 = new PcWareListByCourseId
            {
                videoId = 12,
                IsAuditions = false,
                percent = 23,
                practiceId = 43,
                statuss = 2,
                TotalTime = "12.2",
                wareId = 22,
                wareName = "课件"
            };
            var m2 = new PcWareListByCourseId
            {
                videoId = 12,
                IsAuditions = false,
                percent = 23,
                practiceId = 43,
                statuss = 2,
                TotalTime = "12.2",
                wareId = 22,
                wareName = "课件"
            };
            t.Add(m1);
            t.Add(m2);
            RedisInfoHelper.SetRedis("test",t);

            var get = RedisInfoHelper.GetRedisValue("test");

            var jo = JArray.Parse(get);
            var jj = jo.ToObject<List<PcWareListByCourseId>>();        

实体代码:

    public class PcWareListByCourseId
    {
        public int wareId { set; get; }
        public string wareName { set; get; }
        public bool IsAuditions { set; get; }//试听
        public int videoId { set; get; }
        public int percent { set; get; }
        public int practiceId { set; get; }//练习题id
        public int statuss { set; get; }
        public string TotalTime { set; get; }//11'22"
    }

 

 成功的走到最后一步,转化成功。

 这次用的是JArray这个方法类。

 

posted @ 2016-03-14 13:36  大稳·杨  阅读(11297)  评论(1编辑  收藏  举报