一、什么是 Json
1.JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
2.JSON 是轻量级的文本数据交换格式
3.JSON 独立于语言:JSON 使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON。
4.JSON 具有自我描述性,更易理解
二、Json 格式
{} 表示对象
[] 表示数据
, 表示分割数据
: 表示键值对
每个键值对都代表一条数据,每条数据都是以","隔开的
三、Json 对象
结构:
{
key1:value1,
key2:value2,
key3:value3……
}
1.面向对象:key对应的是对象的变量,值对应的是变量的值
{
"name":"AIVSR",
"age":18
}
2.复杂对象:
对象内包含对象:
{
"name":"AIVSR",
"child": { "name"="c_AIVSR" }
}
对象内包含数组:
{
"name":"AIVSR",
"house":["二环",”三环“,”四环“]
}
对象内包含数组,数组是一个对象数组:
{
"name":"AIVSR",
"house":[{
"name":"h1",
"add":"二环"
},
{
"name":"h2"
"add":"三环"
}]
}
四 、C# 关于Json的操作(简单对象形式)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
#region 模块信息
/*----------------------------------------------------------------
// 模块名:JsonTool
// 创建者:AIVSR
// 创建日期:
// 修改者列表:
// 模块描述:MyFramework 关于Json的操作
//----------------------------------------------------------------*/
#endregion
public class JsonTools : MonoBehaviour
{
/// <summary>
/// 创建简单列Json
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="listJson"></param>
public static void Create(string folder, string name, List<Dictionary<string, string>> listJson, bool isCovered = false)
{
string localPath = ProjectBasicInformation.PlatformPath + folder + "/" + name + ".json";
if (!File.Exists(localPath))
{
JObject jObject = new JObject();
foreach (var jsonDirItem in listJson)
{
foreach (var jsonItem in jsonDirItem)
{
jObject[jsonItem.Key] = jsonItem.Value.ToString();
}
}
string json = jObject.ToString();
if (isCovered)
File.WriteAllText(localPath, json);
}
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
/// <summary>
/// 追加Json
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="listJson"></param>
public static void Add(string folder, string name, List<Dictionary<string, string>> listJson)
{
string localPath = Application.streamingAssetsPath + "/" + folder + "/" + name + ".json";
var content = File.ReadAllText(localPath);
JObject jObject = new JObject();
JObject localJObject = JObject.Parse(content);
foreach (var jsonDir in listJson)
{
foreach (var jsonItem in jsonDir)
{
localJObject.Add(jsonItem.Key, jObject[jsonItem.Key] = jsonItem.Value.ToString());
}
}
if (!File.Exists(localPath))
{
FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.ReadWrite);
fileStream.Close();
}
File.WriteAllText(localPath, localJObject.ToString());
}
/// <summary>
/// 读取json
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <returns></returns>
public static List<Dictionary<string, string>> ReadFile(string folder, string name)
{
string localPath = ProjectBasicInformation.PlatformPath + folder + "/" + name + ".json";
List<Dictionary<string, string>> listJsonDictionary = new List<Dictionary<string, string>>();
if (!File.Exists(localPath))
{
using (StreamReader file = File.OpenText(localPath))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject jObject = (JObject)JToken.ReadFrom(reader);
foreach (var jsonitem in jObject)
{
Dictionary<string, string> readJsonDictionary = new Dictionary<string, string>();
readJsonDictionary.Add(jsonitem.Key, jsonitem.Value.ToString());
Debug.Log(jsonitem);
listJsonDictionary.Add(readJsonDictionary);
}
}
}
}
return listJsonDictionary;
}
/// <summary>
/// 读取json文件中的某个已知Key值
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadKey(string folder, string name, string key)
{
string localPath = ProjectBasicInformation.PlatformPath + folder + "/" + name + ".json";
string jTokenString = "";
if (File.Exists(localPath))
{
using (StreamReader file = File.OpenText(localPath))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject jObject = (JObject)JToken.ReadFrom(reader);
JToken jToken = jObject[key]; //获取Key的值
jTokenString = jToken.ToString();
}
file.Close();
file.Dispose();
}
}
return jTokenString;
}
/// <summary>
/// 更新Json
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="newValue"></param>
public static void Updata(string folder, string name, string key, string newValue)
{
string localPath = Application.streamingAssetsPath + "/" + folder + "/" + name + ".json";
using (StreamReader file = File.OpenText(localPath))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject jObject = (JObject)JToken.ReadFrom(reader);
JToken jToken = jObject[key]; //获取Key的值
string jTokenString = jToken.ToString();
if (newValue != jTokenString)
{
jObject[key] = newValue;
}
file.Close();
file.Dispose();
if (!File.Exists(localPath))
{
FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.ReadWrite);
fileStream.Close();
}
File.WriteAllText(localPath, jObject.ToString());
}
}
}
/// <summary>
/// 删除Json节点
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="key"></param>
public static void Delete(string folder, string name, string key)
{
string localPath = Application.streamingAssetsPath + "/" + folder + "/" + name + ".json";
using (StreamReader file = File.OpenText(localPath))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject jObject = (JObject)JToken.ReadFrom(reader);
if (jObject[key] != null)
{
jObject.Remove(key);
}
file.Close();
file.Dispose();
if (!File.Exists(localPath))
{
FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.ReadWrite);
fileStream.Close();
}
File.WriteAllText(localPath, jObject.ToString());
}
}
}
}
五、常见报错处理:
1、检查花括号,中括号是否能对应上
2、检查你的Json中符号是否都是半角符号,英文符号
3、你的int类型值如果是1,不能写成01的形式
4、检查一些应该标识序列化特性的地方
注:
1.特性“[]”
[System.Serializable]:修饰类上,证明该类是可以被序列化和反序列化的
2.序列化和反序列化
序列化:把对象转换成字节序列的过程称为对象的序列化
反序列化:把字节序列转换成对象的过程称为对象的反序列化