gavanwanggw

导航

对于Json和对象转换的学习

学习这个的用处有非常多的:
       在传输数据过程中比較查看数据比較清晰,代码也较清晰。也能够避免split函数带来的隐藏bug
当然也有不足:
       准备工具较繁琐,须要准备对象(当然一般项目中每张表都会有相应的Model类,少量数据不建议适用)
------请教各位大侠。能不能直接将Json字符串转换为对象(对象格式无需我们自定义,由于Json中已经非常明白对象的格式了)

介绍两种方式:
第一种:
适用.NET Framework3.5以上版本号的
命名空间为using System.Runtime.Serialization.Json;
        //将一个对象转换为Json字符串
        public static string ObjectToJson_(object obj) 
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0,(int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        }

        //将一个Json字符串转换为对象
        public static object JsonToObject_(string jsonString, Type type)
        {
            DataContractJsonSerializer serilizer = new DataContractJsonSerializer(type);
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            return serilizer.ReadObject(stream);
        }
另外一种:
须要引用Newtonsoft.Json.dll 下载地址
        // 从一个对象信息生成Json串
        public static string ObjectToJson(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        // 从一个Json串生成对象信息
        public static object JsonToObject(string jsonString, Type obj)
        {
            return JsonConvert.DeserializeObject(jsonString, obj);
        }

以下运用的实例:
                    //将提交的数据Json转换为Model
                    string sendMessage = "[{\"Type\":\"精美小炒\", \"MyLunch\":[{\"Name\":\"番茄炒蛋\", \"Price\":\"10\"}, {\"Name\":\"耗油牛肉\", \"Price\":\"14\"}, {\"Name\":\"金针菇肥牛\", \"Price\":\"16\"}, {\"Name\":\"虾仁炒蛋\", \"Price\":\"15\"}]}, {\"Type\":\"精美套餐\", \"MyLunch\":[{\"Name\":\"商务套餐\", \"Price\":\"15\"}, {\"Name\":\"红烧猪排套餐\", \"Price\":\"12\"}, {\"Name\":\"椒盐排条套餐\", \"Price\":\"10\"}, {\"Name\":\"茄汁牛排套餐\", \"Price\":\"10\"}]}, {\"Type\":\"特色盖浇饭\", \"MyLunch\":[{\"Name\":\"回锅肉盖浇饭\", \"Price\":\"12\"}, {\"Name\":\"尖椒牛柳盖浇饭\", \"Price\":\"13\"}, {\"Name\":\"蒜苗肉丝盖浇饭\", \"Price\":\"15\"}, {\"Name\":\"辣子鸡盖浇饭\", \"Price\":\"12\"}]}]";
                    List<Meal> getModel = new List<Meal>();
                    getModel = (List<Meal>)JsonToObject_(sendMessage, typeof(List<Meal>));

                    //将Model转换为Json
                    List<Meal> MealList = new List<Meal>();
                    Meal modelMeal = new Meal();
                    List<Lunch> myLunch = new List<Lunch>();
                    Lunch modelLunch = new Lunch();
                    modelLunch.Name = "台式卤肉";
                    modelLunch.Price = "15";
                    myLunch.Add(modelLunch);
                    modelMeal.MyLunch = myLunch;
                    modelMeal.Type = "星期五套餐";
                    MealList.Add(modelMeal);

                    string jsonString = ObjectToJson_(MealList);
                    result = jsonString;

大概就是这样子了,错误求指正!

QAQ


posted on 2017-04-28 19:41  gavanwanggw  阅读(122)  评论(0编辑  收藏  举报