C#:
解析:
解析工具:LitJson,JsonConvert
1.首先附上我们的Json文档
{
"Name": "evelyn",
"Level": 33,
"Age": 31,
"SkillList": [
{
"id": 1,
"name": "格挡",
"ssl": 100
},
{
"id": 2,
"name": "定身",
"ssl": 50
},
{
"id": 3,
"name": "流血",
"ssl": 45
}
]
}
2.先把我们的Json文档进行C#转换,这里有网站可以帮助我们进行转换:https://sandbox.runjs.cn/show/nhuozgst
public class Root
{
public string Name { get; set; }
public int Level { get; set; }
public int Age { get; set; }
public List<SkillLists> SkillList { get; set; }
}
public class SkillLists
{
public int id { get; set; }
public string name { get; set; }
public int ssl { get; set; }
}
3.实现代码:
static void LitJsonAndJsonConvert_Parse()
{
//LitJson解析
string path = File.ReadAllText("F:/_VS_Json/_VS_Json/JsonT.json");
Root date = JsonMapper.ToObject<Root>(path);
Console.WriteLine("Name" + date.Name);
Console.WriteLine("Age" + date.Level);
Console.WriteLine("Level" + date.Age);
foreach (var item in date.SkillList)
{
Console.WriteLine();
Console.WriteLine("{0}:{1}", "id", item.id);
Console.WriteLine("{0}:{1}", "name", item.name);
Console.WriteLine("{0}:{1}", "ssl", item.ssl);
}
//JsonConvert流解析
string ss = Newtonsoft.Json.JsonConvert.SerializeObject(date);
Console.WriteLine(ss);
}
写入:
1.我们采用重新赋值的方式写入,同样是我们的一个Root类和数组
static void LitJson_Write()
{
Root root = new Root();
root.Age = 1;
root.SkillList = new List<SkillLists>();
SkillLists skill = new SkillLists();
skill.id = 1;
skill.name = "Json";
skill.ssl = 10;
root.SkillList.Add(skill);
string s = JsonMapper.ToJson(root);
File.WriteAllText("E:/AD.txt", s, Encoding.UTF8);//写入创建的新文本里
}
2. 第一种写入方法给Root类的数据重新赋值,下面这种方法是我们自己手动添加键值对。
(个人觉得用键值对表示比较好....)
代码:
static void JsonData_Write()
{
JsonData data = new JsonData();
data["name"] = "大白";//直接给键赋值
string str = data.ToJson();
File.WriteAllText(@"E:/ads.txt", str , Encoding.UTF8);//写入创建的新文本里
}
如果按上述代码写入的话我们写入的文档的值是正则表达式,如图:
文本里选中的地方就是我们写入的“大白”,但是存到.txt文本里就变成了正则表达式。原因是.txt文本格式是Unicode,如果想让正则表达式变成我们正常的中文就只需把写入的文本格式转换成UTF-8就行了。
代码:
static void JsonData_Write()
{
JsonData data = new JsonData();
data["name"] = "大白";//直接给键赋值
string str = data.ToJson();
Regex regex = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");//把Unicode转换为UTF-8格式(正则表达式)
string ss = regex.Replace(str, delegate (Match m)
{
return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
});//
File.WriteAllText(@"E:/ads.txt", ss, Encoding.UTF8);//写入创建的新文本里
}
———————————————————————————————————————————————————————————————————————————分割线—————————————————————————
完整代码:
using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Json;
using System.Text.RegularExpressions;
namespace _VS_Json
{
public class Root
{
public string Name { get; set; }
public int Level { get; set; }
public int Age { get; set; }
public List<SkillLists> SkillList { get; set; }
}
public class SkillLists
{
public int id { get; set; }
public string name { get; set; }
public int ssl { get; set; }
}
class Program
{
static void Main(string[] args)
{
LitJson_Write();
JsonData_Write();
Console.ReadKey();
}
static void LitJson_Parse()
{
string path = File.ReadAllText("F:/学习历程啊/1702-03七月各种解析(除了XML)/_VS_Json/_VS_Json/JsonT.json");
Root date = JsonMapper.ToObject<Root>(path);
Console.WriteLine("Name" + date.Name);
Console.WriteLine("Age" + date.Level);
Console.WriteLine("Level" + date.Age);
foreach (var item in date.SkillList)
{
Console.WriteLine();
Console.WriteLine("{0}:{1}", "id", item.id);
Console.WriteLine("{0}:{1}", "name", item.name);
Console.WriteLine("{0}:{1}", "ssl", item.ssl);
}
}
static void LitJson_Write()
{
Root root = new Root();
root.Age = 1;
root.SkillList = new List<SkillLists>();
SkillLists skill = new SkillLists();
skill.id = 1;
skill.name = "Json";
skill.ssl = 10;
root.SkillList.Add(skill);
string s = JsonMapper.ToJson(root);
File.WriteAllText("E:/AD.txt", s, Encoding.UTF8);//写入创建的新文本里
}
static void JsonData_Write()
{
JsonData data = new JsonData();
data["name"] = "大白";//直接给键赋值
string str = data.ToJson();
Regex regex = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");//把Unicode转换为UTF-8格式(正则表达式)
string ss = regex.Replace(str, delegate (Match m)
{
return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
});//
File.WriteAllText(@"E:/ads.txt", str, Encoding.UTF8);//写入创建的新文本里
}
}
}
PS:!!!!!!!解析的Json文档在第一条(最上面)!!!!!!!
Unity:
首先我们需要做的准备是导入LetJson.dll文件。这里一定要注意版本,如果找不到合适的版本就用VS去创建一个合适的类库的项目工程,在这个工程里下载,然后再导入到Unity里。
附上代码:
static string path = Application.dataPath + "/Resouces/Json.json";
[MenuItem("Json/Creat")]
static void JsonCreatFunction()
{
data["周董"] = "Jay";
string str = data.ToJson();
if (!File.Exists(path))
{
File.Create(path);
}
File.WriteAllText(path, str);
}