字符串与Json操作

        #region 列表序列化成list
        protected List<T> ToList<T>(string[] listStr)
        {
            var list = new List<T>();
            foreach (var add in listStr)
            {
                if (!string.IsNullOrEmpty(add))
                {
                    // T t = SerializeKit.JsonDeserialize<T>(add);
                    //object对象反序列化
                    JObject jsonBody = JObject.Parse(add);
                    JsonSerializer js = new JsonSerializer();
                    T t = js.Deserialize<T>(jsonBody.CreateReader());
                    list.Add(t);
                }
            }
            return list;
        }

        protected List<T> ArrayToList<T>(string listStr)
        {
            
            List<T> list = new List<T>();
            if (!string.IsNullOrEmpty(listStr))
            {
                JArray jsonRsps = JArray.Parse("[" + listStr + "]");
                for (int i = 0; i < jsonRsps.Count; i++)
                {
                    JsonSerializer js = new JsonSerializer();
                    T obj = js.Deserialize<T>(jsonRsps[i].CreateReader());
                    list.Add(obj);
                }
            }
            return list;
        }

        #endregion

        #region JsonToString[]
        protected string[] JsonToString(string jsontext)
        {
            JObject jo = JObject.Parse(jsontext);
            string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray();

            return values;
        }
        #endregion

 

posted on 2013-06-04 14:37  wboweb  阅读(285)  评论(0编辑  收藏  举报

导航