zoukankan      html  css  js  c++  java
  • 对于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


  • 相关阅读:
    Linux的进程线程及调度
    Linux-485收发切换延迟的解决方法
    FFmpeg内存IO模式(内存区作输入或输出)
    ffplay源码分析7-播放控制
    ffplay源码分析6-音频重采样
    ffplay源码分析5-图像格式转换
    ffplay源码分析4-音视频同步
    ffplay源码分析3-代码框架
    ffplay源码分析2-数据结构
    ffplay源码分析1-概述
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6783051.html
Copyright © 2011-2022 走看看