asp网站gzip压缩,扬中网站建设机构,wordpress搭建本地博客,微信代运营合同序列化是指将对象转换为可存储或传输的格式#xff0c;例如将对象转换为JSON字符串或字节流。反序列化则是将存储或传输的数据转换回对象的过程。这两个过程在数据持久化、数据交换以及与外部系统的通信中非常常见
把对象转换成josn字符串格式 这个过程就是序列化 josn字符…序列化是指将对象转换为可存储或传输的格式例如将对象转换为JSON字符串或字节流。反序列化则是将存储或传输的数据转换回对象的过程。这两个过程在数据持久化、数据交换以及与外部系统的通信中非常常见
把对象转换成josn字符串格式 这个过程就是序列化 josn字符串: 就是拥有固定格式的字符串 { name: 张三, age: 25, isStudent: true }
步骤:
1.在引用上右键
2.添加Nuget程序包
3.搜索json
4.点击下载Newtonsoft.json
5.在项目using Newtonsoft.json 先声明两个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 序列反序列.Models
{[Serializable]//表示这个类可以序列化internal class User{public int Id { get; set; }public string Name { get; set; }public string Email { get; set; }public string Password { get; set; }}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 序列反序列.Models
{internal class Product{public int Id { get; set; }public string Name { get; set; }public string Description { get; set; }public float Price { get; set; }public User SaleUser { get; set; }}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 序列反序列.Models;
using Newtonsoft.Json;namespace 序列反序列
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}string sstring.Empty;private void button1_Click(object sender, RoutedEventArgs e){Console.ForegroundColor ConsoleColor.Red;User user new User() { Id 1 , Name 小张 ,Email163.com,Password123566};sJsonConvert.SerializeObject(user);Console.WriteLine(s);Console.WriteLine(这上面是序列化的);}private void Button_Click(object sender, RoutedEventArgs e){if (!string.IsNullOrWhiteSpace(s)){User newUser JsonConvert.DeserializeObjectUser(s);Console.WriteLine(newUser.Name);}else{Console.WriteLine(先序列化);}}}
}序列化 Console.ForegroundColor ConsoleColor.Red;User user new User() { Id 1 , Name 小张 ,Email163.com,Password123566};sJsonConvert.SerializeObject(user);Console.WriteLine(s);Console.WriteLine(这上面是序列化的);
反序列化 if (!string.IsNullOrWhiteSpace(s)){User newUser JsonConvert.DeserializeObjectUser(s);Console.WriteLine(newUser.Name);}else{Console.WriteLine(先序列化);}
效果 对象嵌套序列化
Product product new Product()
{Id 1,Name 小明,Description 小明有一个狗,Price 1000.123f,SaleUser new User { Name 小兰, Id 2 }
};string s JsonConvert.SerializeObject(product, Formatting.Indented);
Console.WriteLine(s);
Product product1 JsonConvert.DeserializeObjectProduct(s);
Console.WriteLine(product1.Name);
Console.WriteLine(product1.SaleUser.Name);
数组序列化 User user1 new User(){Id 1,Name 小丑,Email 168qq.com,Password 1452};User user2 new User() { Id 2, Name 效率, Email 9999qq.com, Password 246810 };object[] objs { user1, user2, true, 不知道 };s2 JsonConvert.SerializeObject(objs, Formatting.Indented);Console.WriteLine(s2);
数组反序列化 if (!string.IsNullOrWhiteSpace(s2)){object[] objs JsonConvert.DeserializeObjectobject[](s2);Console.WriteLine(objs[1]);//字符串User u JsonConvert.DeserializeObjectUser(objs[1].ToString());Console.WriteLine(u.Name);}
效果
文章转载自: http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.fmry.cn.gov.cn.fmry.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn