美文网首页
解析jason

解析jason

作者: 价值投机168 | 来源:发表于2019-10-28 13:27 被阅读0次

    System.Web.Extensions引用。

    string JsonData = "[{\"100\":\"1.0.0\"},{\"101\":\"2.2.2\"}]";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string,object> json = (Dictionary<string, object>)serializer.DeserializeObject(JsonData);//这个不能解析上面的字符串,上面的字符串是list呀
            string firstKey = json.ElementAt(0).Key;
            string secondKey = json.ElementAt(1).Key;
    
            object[] jsonInnerObj = (object[])serializer.DeserializeObject(JsonData);
            //强制类型转化
            Dictionary<string, object> jsonLast = (Dictionary<string, object>)jsonInnerObj[0]; 
    

    下面是一个更完整的例子,注意:这个是数组,不只是object的例子:

    string JsonData = "[{\"100\":\"1.0.0\"},{\"101\":\"2.2.2\"}]";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            object[] jsonInnerObj = (object[])serializer.DeserializeObject(JsonData);
    
            Dictionary<string, string>[] Content = { new Dictionary<string, string>() };
    
            foreach (var j in jsonInnerObj)
            {
                Dictionary<string, object> jsonLast = (Dictionary<string, object>)j;
                for (int i = 0; i < jsonLast.Count; i++)
                {
                    var item = jsonLast.ElementAt(i);//获取字典的下标为i的<key,value>值
                    string itemKey = item.Key;  //获取上面得到的key值
                    string itemValue =(string) item.Value;//获取上面得到的value值
                    Console.WriteLine(itemKey + ":" + itemValue);
    
                    Content[0].Add(itemKey, itemValue);
                }
            }
    
            string Contentjson = serializer.Serialize(Content);
            Console.WriteLine(Contentjson);
    
            Console.ReadKey();
    

    上面的例子有点问题,没有完全还原,下面的例子完全还原了:

    string JsonData = "[{\"100\":\"1.0.0\"},{\"101\":\"2.2.2\"}]";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            object[] jsonInnerObj = (object[])serializer.DeserializeObject(JsonData);
    
            List<object> Content =  new List<object>() ;
    
            foreach (var j in jsonInnerObj)
            {
                Dictionary<string, object> jsonLast = (Dictionary<string, object>)j;
                for (int i = 0; i < jsonLast.Count; i++)
                {
                    var item = jsonLast.ElementAt(i);//获取字典的下标为i的<key,value>值
                    string itemKey = item.Key;  //获取上面得到的key值
                    object itemValue =(string) item.Value;//获取上面得到的value值
                    Console.WriteLine(itemKey + ":" + itemValue);
    
                    Dictionary<string, object> b = new Dictionary<string, object>();
                    b[itemKey] = itemValue;
                    Content.Add(b);
                }
            }
    
            string Contentjson = serializer.Serialize(Content);
            Console.WriteLine(Contentjson);

    相关文章

      网友评论

          本文标题:解析jason

          本文链接:https://www.haomeiwen.com/subject/yeavvctx.html