crm开发定制c#学习—JSON文件及解析

一、JSON文件介绍

JSONcrm开发定制是存储和交换文本的语法,类似于
但比XML更小,crm开发定制更容易解析,于XMLcrm开发定制一样是一种数据格式
JSONcrm开发定制是一种轻量级的数据交换格式,crm开发定制采用完全独立于语言的文本格式,crm开发定制更容易编写以及解析
例如

[{"id":2,"name":"星河爆破","number":999},{"id":3,"name":"九星连珠","number":9},{"id":4,"name":"一语成谶","number":999}]
  • 1
  • 2
  • 3
  • 4
  • 5

二、JSO语法规则

crm开发定制数据保存在键值对中
crm开发定制数据由逗号分割
花括号保存对象
方括号保存数组

三、引入JSON库文件

JSON解析器和JSON库支持许多不同的编程语言
能对json文件进行解析的有很多,详见JSON官网,http://www.json.org/json-en.html
在主要是使用的是LitJSON或者Newtonsoft.Json,LitJSON使用NuGet安装,界面如下

四、利用JSON.Mapper去解析JSON文件

4.1 JSON为数组

原始json内容
json.txt:

[{"id":2,"name":"星河爆破","number":999},{"id":3,"name":"九星连珠","number":9},{"id":4,"name":"一语成谶","number":999}]
  • 1
  • 2
  • 3
  • 4
  • 5

思想:

1.利用File去读取json文件2.通过JsonMapper转为对象(jsondata)3.使用索引,遍历jsondata进行输出
  • 1
  • 2
  • 3

代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;//引入LitJsonusing LitJson;namespace json操作{    class Program    {        static void Main(string[] args)        {            //JsonMapper类            //ToObject方法可以将一个json字符串进行解析,            //解析的结果是json.date            //我们使用jsonMapper去解析json文本            //jsondata代表一个数据或者对象            //json217.txt为数组[],所以jsonData为数组            //注意json文件位置,需要在bug目录下            JsonData jsonData =JsonMapper.ToObject(File.ReadAllText("json217.txt")) ;            //使用foreach去遍历数组信息            //temp也是jsondata类型的,但是temp是一个对象            foreach (JsonData temp in jsonData)            {                //通过索引其去取得temp中的value                //返回值还是jsondata类型                JsonData idvalue = temp["id"];                JsonData namevalue = temp["name"];                JsonData numbervalue = temp["number"];                //将jsondata转化为字符串进行输出                //name本身就为str,所以不用转换                int id = Int32.Parse(idvalue.ToString());                int number = Int32.Parse(numbervalue.ToString());                //输出                Console.WriteLine(id+":"+ namevalue + ":" + number);            }            Console.ReadKey();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

运行结果

4.2 JSON为数组,使用实体类

原始json内容
json217.txt:

[{"id":2,"name":"星河爆破","number":999},{"id":3,"name":"九星连珠","number":9},{"id":4,"name":"一语成谶","number":999}]
  • 1
  • 2
  • 3
  • 4
  • 5

思想:

1.根据JSON文件格式,建立实体类,例如根据json217.txt建立实体类Magic,包含id、name、number三个字段以及一个方法2.根据json格式,新建list,使用Magic实体类作为泛型,新建magicList类List<Magic> magicList = new List<Magic>();3.通过JsonMapper转为对象(jsondata),赋值到magicList中4.对magicList进行遍历输出
  • 1
  • 2
  • 3
  • 4
  • 5

新建一个Magic实体类用于解析
Magic.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace json操作{    class Magic    {        public int id;        public string name;        public int number;        public override string ToString()        {            return string.Format("id:{0},name:{1},numebr:{2}",id,name,number);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;//引入LitJsonusing LitJson;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace json操作{    class Program    {        static void Main(string[] args)        {            //因为json文件为集合,所以新建一个集合            List<Magic> magicList = new List<Magic>();            //依旧使用JsonMapper去进行解析            JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json217.txt"));            foreach (JsonData temp in jsonData)            {                //创建一个新对象                Magic magic = new Magic();                //通过索引其去取得temp中的value                //返回值还是jsondata类型                JsonData idvalue = temp["id"];                JsonData namevalue = temp["name"];                JsonData numbervalue = temp["number"];                //将jsondata转化为字符串进行输出                //name本身就为str,所以不用转换                int id = Int32.Parse(idvalue.ToString());                int number = Int32.Parse(numbervalue.ToString());                magic.id = id;                magic.number = number;                magic.name = namevalue.ToString();                magicList.Add(magic);            }            foreach (var temp in magicList)                 {                Console.WriteLine(temp);            }            Console.ReadKey();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

运行结果

可以发现,结果是一样的,但是如此做会增加代码量,不够方便,下面介绍通过泛型去解析JSON

五、利用Json.MApper结合泛型去解析JSON文件

5.1JSON为数组

原始json内容
json217.txt:

[{"id":2,"name":"星河爆破","number":999},{"id":3,"name":"九星连珠","number":9},{"id":4,"name":"一语成谶","number":999}]
  • 1
  • 2
  • 3
  • 4
  • 5

思想:

1.对JSON文件进行解析,发现为外层为数组格式,可以认为是一个Magic对象数组2.使用泛型去解析,用<>表示泛型,如下一个Magic对象数组表示为Magic[]JsonMapper.ToObject<Magic[]>注意:json里面的键必须与实体类对应3.遍历Magic[]
  • 1
  • 2
  • 3
  • 4
  • 5

代码:

namespace json操作{    class Program    {        static void Main(string[] args)        {            //使用泛型去解析json            //因为读取的文件格式为magic实体类的数组,所以返回是个magic的数组            //注意实体类需要对应上,            Magic[] magicArray=JsonMapper.ToObject<Magic[]>(File.ReadAllText("json217.txt"));            foreach(var temp in magicArray)             {                Console.WriteLine(temp);            }            Console.Read();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果:

5.2JSON为数组,使用集合去解析

思想:

1.对JSON文件进行解析,发现为外层为数组格式,也可以认为是个集合2.使用泛型去解析,用<>表示泛型,如下一个Magic对象数组表示为Magic[]JsonMapper.ToObject<Magic[]>改为JsonMapper.ToObject<List<Magic>>注意:json里面的键必须与实体类对应3.遍历List<Magic
  • 1
  • 2
  • 3
  • 4
  • 5

代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;//引入LitJsonusing LitJson;namespace json操作{    class Program    {        static void Main(string[] args)        {            //任何可以数组的地方都可以使用一个集合            List<Magic> magicklist = JsonMapper.ToObject<List<Magic>>(File.ReadAllText("json217.txt"));            foreach (var temp in magicklist)            {                Console.WriteLine(temp);            }            Console.Read();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

运行结果:

5.3JSON为对象

原始json内容
TextFlie.txt:

{ "status": 1, "apiId": "89757", "date": "2022 / 01 / 24 13: 12: 10", "message": null, "devicelist": [{				"productcode": "126345",				"devicecode": "15632478",				"url": "http://sssss/sssss/ssss/ssss"				},				{				"productcode": "222222",				"devicecode": "222222",				"url": "http://sssss/sssss/ssss/ssss"				},				{				"productcode": "333333",				"devicecode": "333333",				"url": "http://sssss/sssss/ssss/ssss"				}		]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

思想:

1.对JSON文件进行解析,发现为整体为对象,对象中包括status、apiId、date、message、devicelist属性2.建立实体类模型,新建类IotDevice类,分为IotDevice类和DevicelistItem类IotDevice类包含:status、apiId、date、message、devicelist(继承与DevicelistItem)DevicelistItem类包含:productcode、devicecode、url有点像俄罗斯套娃的感觉3.使用json.mapper<实体类>进行解析,返回值为实体类的对象重点:建立实体类
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

IotDevice.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace json操作{    public class DevicelistItem    {        public string productcode { get; set; }        public string devicecode { get; set; }        public string url { get; set; }        public override string ToString()        {            return string.Format("productcode:{0},devicecode:{1},url:{2}", productcode, devicecode, url);        }    }    public class IotDevice    {        public int status { get; set; }        public string apiId { get; set; }        public string date { get; set; }        public string message { get; set; }        public List<DevicelistItem> devicelist { get; set; }        public override string ToString()        {            return string.Format("status:{0},apiId:{1},date:{2},message:{3},devicelist:{4},", status, apiId, date, message, devicelist);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;//引入LitJsonusing LitJson;namespace _032json操作{    class Program    {        static void Main(string[] args)        {            //json文档整体为IotDevice类型,所以转换的时候泛型为<IotDevice>            //返回值为IotDevice对象            IotDevice iotdevice = JsonMapper.ToObject <IotDevice>(File.ReadAllText("TextFile1.txt"));            Console.WriteLine(iotdevice);            foreach (var temp in iotdevice.devicelist)             {                Console.WriteLine(temp);            }            Console.Read();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

运行结果:

5.4复杂的JSON文件解析

原始json内容
TextFlie2.txt:

在这里插入代码片
  • 1

六、字符串转化为JSON

代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;//引入LitJsonusing LitJson;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace json操作{    class Program    {        static void Main(string[] args)        {            //新建IotDevice的实体类            IotDevice device1 = new IotDevice();            device1.date = "2022/02/17";            device1.apiId = "89757";            //转为json的字符串            string json = JsonMapper.ToJson(device1);            Console.WriteLine(json);            Console.Read();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

运行结果:

七、JSON常用工具

1.JSON格式校验工具

可以用来对写的JSON文档进行校验,确保书写正确
地址:https://www.bejson.com/

2.JSON数据生成c#实体类

当JSON设计多层时,可以使用工具,自动生成实体类模板
地址:https://www.bejson.com/convert/json2csharp/

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发