menu 首页 标签 归档 视频 关于
CSharp-Json的序列化与反序列化

一言加载中...

JSON介绍

Json是一种轻量数据交互格式,易于阅读,编写,基于ECMAScript 的一个子集,是一种独立语言。为了数据传输方便,传输时,将要传输的对象序列化为Json,效率极高。接收时,通过反序列化转化成对象。与XML语言效果一样。两者可以相互代替。但比XML更简洁,更容易阅读,传输效率更高。

语法规则

  • 数据保存在键值对中
  • 数据由逗号分隔
  • 花括号保存对象
  • 方括号保存数组
  • 值可以是双引号括起来的字符串、数值(number)、true、false、 null、对象或者数组(array)

1. 使用LitJson进行序列化与反序列化

序列化

public class SkillBean
{
    public int id { get; set; }
    public string name { get; set; }
    public int damage { get; set; }
}
var SkillList = new List<SkillBean>();
SkillBean skill1 = new SkillBean &#123; id = 101, name = "落叶飞花", damage = 500 &#125;;
SkillBean skill2 = new SkillBean &#123; id = 102, name = "暗度陈仓", damage = 304 &#125;;
SkillBean skill3 = new SkillBean &#123; id = 103, name = "万丈光芒", damage = 302 &#125;;
SkillBean skill4 = new SkillBean &#123; id = 104, name = "十方皆杀", damage = 405 &#125;;
SkillBean skill5 = new SkillBean &#123; id = 105, name = "银光落刃", damage = 220 &#125;;
SkillBean skill6 = new SkillBean &#123; id = 106, name = "幻影剑舞", damage = 690 &#125;;
SkillBean skill7 = new SkillBean &#123; id = 107, name = "狂龙紫电", damage = 666 &#125;;

SkillList.Add(skill1);
SkillList.Add(skill2);
SkillList.Add(skill3);
SkillList.Add(skill4);
SkillList.Add(skill5);
SkillList.Add(skill6);
SkillList.Add(skill7);

string json = JsonMapper.ToJson(SkillList);
Console.WriteLine(json);

输出结果(汉字自动转化为Unicode):

反序列化

  • 类型一:
[
  &#123;"id":1,"name": "天下无双","damage": 100&#125;,
  &#123;"id":2,"name": "落叶飞花","damage": 200&#125;,
  &#123;"id":3,"name": "咫尺天涯","damage": 300&#125;
]
class SkillBean
&#123;
    public int id &#123; get; set; &#125;
    public string name &#123; get; set; &#125;
    public int damage &#123; get; set; &#125;
&#125;
//解析
//获取Json字符串
string json = File.ReadAllText("SkillInfo.json");

List<SkillBean> skillList = new List<SkillBean>();
//解析得到skill对象集合
JsonData jsonDataList = JsonMapper.ToObject(json);
foreach (JsonData item in jsonDataList)
&#123;
    SkillBean skill = new SkillBean();
    skill.id = Int32.Parse(item["id"].ToString());
    skill.name = item["name"].ToString();
    skill.damage = Int32.Parse(item["damage"].ToString());
    skillList.Add(skill);
&#125;
  • 类型二:
&#123;
  "SkillList": [
    &#123; "id": 1, "name": "天下无双", "damage": 100 &#125;,
    &#123; "id": 2, "name": "落叶飞花", "damage": 200 &#125;,
    &#123; "id": 3, "name": "咫尺天涯", "damage": 300 &#125;
  ]
&#125;
public class SkillBeanList
&#123;
    public SkillBean[] SkillList &#123; get; set; &#125;
&#125;

public class SkillBean
&#123;
    public int id &#123; get; set; &#125;
    public string name &#123; get; set; &#125;
    public int damage &#123; get; set; &#125;
&#125;
string json = File.ReadAllText("SkillInfo.json");
var list = JsonMapper.ToObject<SkillBeanList>(json).SkillList;

2. 使用FastJson进行序列化与反序列化

fastJson序列化,反序列化,都只需要一行代码。且运行速度快.

序列化

string json = fastJSON.JSON.ToJSON(SkillList);

反序列化

JsonBean

public class Rootobject
&#123;
    public SkillBean[] SkillList &#123; get; set; &#125;
&#125;

public class SkillBean
&#123;
    public int id &#123; get; set; &#125;
    public string name &#123; get; set; &#125;
    public int damage &#123; get; set; &#125;
&#125;
string json = File.ReadAllText("skillInfo.json");

var Jobject = fastJSON.JSON.ToObject<Rootobject>(json).SkillList;
foreach (var item in Jobject)
&#123;
    Console.WriteLine(item.id + " " + item.name + " " + item.damage);
&#125;

参考

写博客不易,请我喝杯咖啡?

评论

arrow_upward