C#Json文件解析,寫入,正則表示式轉換。Unity建立Json文件以及寫入。
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);
}