读取Json

        private void DeserializeJson()
        {
            string content = "";
            string input = "{'key1':{'Name':'ZhangSan','Age':20},'key2':{'Name':'LiSi','Age':21},'key3':{'Name':'WangWu','Age':22}}";
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> jsonList = serializer.DeserializeObject(input) as Dictionary<string, object>;
                foreach (KeyValuePair<string, object> obj in jsonList)
                {
                    string valText = "";
                    Dictionary<string, object> property = obj.Value as Dictionary<string, object>;
                    foreach (KeyValuePair<string, object> pro in property)
                    {
                        valText += string.Format("{0}{1}:{2}", string.IsNullOrEmpty(valText) ? "" : ",", pro.Key, pro.Value);
                    }
                    content += string.Format("{0}:[{1}]\n", obj.Key, valText);
                }

            }
            catch (ArgumentException)
            {
                throw new ArgumentException("输入Json字符串有误,请重新输入!");
            }
            catch (NullReferenceException)
            {
                throw new NullReferenceException("未能找到可用对象!");
            }
        }
posted @ 2012-07-24 20:07  清山博客  阅读(212)  评论(0编辑  收藏  举报