一、json
1、json 是一种轻量级的数据交换格式
2、json 包含:值、对象、数组
(1)、JSON值可以为:字符串、数值、布尔值、对象、数组、空
(2)、JSON对象:
- 用 { } 括起来
- 键值对,中间用:分开
- 用逗号分割各个键值对
如: { 名称1 : 值1 , 名称2 : 值2 }
(3)、 json 的基本格式:对象格式和数组格式,其他的各种都可以为基本格式的组合、嵌套。
// 1、对象格式
{
"name": "Jeff",
"age": 18,
"地址": "上海"
}
// 2、数组格式
[
{ "name01": "Hello" },
{ "name02": "Karl" },
{"name03": "Kity"}
]
【注意】:使用数组时,每个元素的类型都相同。否则可以使用对象
3、JSON 字符串的序列化与反序列化
(1)、序列化:即将指定的对象转换成JSON字符串。其目的是为了传输信息、用来写入JSON字符串。此时是为了计算机进行操作。
(2)、反序列化:即将JSON 转换成指定的.NET类型(如类、结构体)。其目的的是打印及显示JSON 字符串,用于读取JSON文件。
二、使用C#解析JSON
1、方案一:使用 JavaScriptSerializer
(1)、添加 JavaScriptSerializer 到项目中,右键点击该项目下的 ”引用“,然后分别选择如下内容:
(2)、然后在项目的开头添加:using System.Web.Script.Serialization; 才可以使用 JavaScriptSerializer 来对 json 字符串进行反序列化操作。同时使用了 IO,所以也需要 using System.IO;
(3)、json文件
(4)、对应代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.IO;
namespace TheJson
{
/// <summary>
/// 该Person类只是一个普通的演示类
/// </summary>
class Person
{
public string name;
public int age;
// 一个用于重写的字符串,以便在控制台上展示最后的结果
public override string ToString()
{
return string.Format("Name:{0},\nAge:{1}", name, age);
}
}
class Program
{
static void Main(string[] args)
{
string path1 = @"D:\Temp\UnityTxtWriteLoad\JSON.json";
// 将path1中的json文件读取,并放入数据流中(字符串)
string JSONstring = File.ReadAllText(path1);
//实例化一个JavaScriptSerializer 的例子
JavaScriptSerializer ser = new JavaScriptSerializer();
// 反序列化:将指定的JSONstring字符串,转换成Person类型的对象
Person p1 = ser.Deserialize<Person>(JSONstring);
Console.WriteLine(p1);
Console.ReadKey();
}
}
}
(5)、解析结果
2、方案二:使用 JSON.NET 库
(1)、为了使用 json.net 库,需要添加如下
(2)、在代码的头文件中引用:using Newtonsoft.Json; 以便对json文件进行处理。
由于还需要使用File类,所以还需要引用:using System.IO;
(3)、json 文件
(4)、对应代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace TheJson
{
/// <summary>
/// 该Person类只是一个普通的演示类
/// </summary>
class Person
{
public string name;
public int age;
public string gender;
}
class Program
{
static void Main(string[] args)
{
string path1 = @"D:\Temp\UnityTxtWriteLoad\JSON.json";
// 直接从指定文件中反序列化JSON
string JSONstring = File.ReadAllText(path1);
Person p1 = JsonConvert.DeserializeObject<Person>(JSONstring);
Console.WriteLine("姓名:{0},\n性别:{1},\n年龄:{2}",p1.name,p1.gender,p1.age);
Console.ReadKey();
}
}
}
(5)、解析结果
三、使用C#生成JSON
在上面的步骤中,已经将操作 JSON 使用的两种方式,所对应库文件都安装好了。接下来同样使用这两种方式来进行生成 JSON 字符串。
1、方案一:使用 JavaScriptSerializer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization; // 包含该引用
using System.IO;
namespace TheJson
{
/// <summary>
/// 该Person类只是一个普通的演示类
/// </summary>
class Person
{
public string name;
public int age;
public override string ToString()
{
return string.Format("Name:{0},\nAge:{1}", name, age);
}
}
class Program
{
static void Main(string[] args)
{
string path2 = @"D:\Temp\UnityTxtWriteLoad\Output.json";
// 实例化一个JavaScriptSerializer类的对象,通过该对象来对JSON进行序列化和反序列化操作
JavaScriptSerializer ser = new JavaScriptSerializer();
// 实例化一个Person类
Person p2 = new Person() { name = "Jeff", age = 18 };
// 通过JavaScriptSerializer类的对象进行序列化操作
string outputJSON = ser.Serialize(p2);
// 将JSON字符串写入到指定的文件中
File.WriteAllText(path2, outputJSON);
}
}
}
2、方案二:使用 JSON.NET 库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // 包含该引用
using System.IO;
namespace TheJson
{
/// <summary>
/// 该Person类只是一个普通的演示类
/// </summary>
class Person
{
public string name;
public int age;
public override string ToString()
{
return string.Format("Name:{0},\nAge:{1}", name, age);
}
}
class Program
{
static void Main(string[] args)
{
string path2 = @"D:\Temp\UnityTxtWriteLoad\Output.json";
Person p3 = new Person() { name = "Kity", age = 25 };
// 将指定的对象序列化成JSON 字符串
string outputJSON = JsonConvert.SerializeObject(p3);
// 将JSON字符串写入到指定的文件中
File.WriteAllText(path2, outputJSON);
}
}
}
四、应用实例:JSON应用程序数据存储
1、基本要求:
- 启动时从JSON 文件读取应用程序数据
- 通过控制台输入编辑数据
- 退出时编辑JSON 数据文件
2、实例代码