本篇介绍在Unity中读写Json文件的两种方式:1.JsonUtility;2.LitJson;
注意:在使用LitJson前需要先将文件拖入unity项目中
一、JsonUtility与LitJson的区别
1.JsonUtility使用自定义类需要加特性,而LitJson不需要
2.JsonUtility支持私有和保护变量,而LitJson不支持
3.JsonUtility不支持字典,而LitJson支持,但是当字典在序列化时如果键含int等非string类型,反序列化时会读取不到,因为序列化后非string类型的键可以理解为已经被改为string类型(但事实不是,没改,但可以当做去理解,想搞清原因可以自己去查一下)
4.JsonUtility不能直接将数组反序列化,而LitJson可以,比如{ {1,2},{3,4}}
5.LitJson需要无参构造,但JsonUtility不需要
6.LitJson存储null时存储的就是null,但JsonUtility存储的是默认值
二、JsonUtility的使用
2.1读取json
string jsonstr2=File.ReadAllText(Application.persistentDataPath + "/" + "testJson.json");
Debug.Log(jsonstr2);
PlayerInfo playerInfo=new PlayerInfo();
playerInfo=JsonUtility.FromJson<PlayerInfo>(jsonstr2);
2.2写入json
[Serializable]
public class Message
{
public int id;
public int num;
public Message(int id,int num)
{
this.id = id;
this.num = num;
}
}
public class Cherry
{
public string name;
public int atk;
public int def;
public float movespeed;
public double roundspeed;
public Message weapon;
public List<int> listInt;
public List<Message> itemList;
[SerializeField]
private int privateI = 1;
[SerializeField]
protected int protectedI = 2;
}
public class JsonUtilityTest: MonoBehaviour
{
void Start()
{
Cherry cherry=new Cherry();
cherry.name = "Cherry";
cherry.atk = 10;
cherry.def = 5;
cherry.movespeed = 20.5f;
cherry.roundspeed = 21.4f;
cherry.weapon = new Message(1, 1);
cherry.listInt = new List<int>() { 1, 2, 3, 4, 5 };
cherry.itemList = new List<Message>() { new Message(1,99),new Message(2,44)};
SaveData(cherry);
}
public void SaveData(Cherry cherry)
{
string jsonStr=JsonUtility.ToJson(cherry);
Debug.Log(Application.persistentDataPath);
File.WriteAllText(Application.persistentDataPath + "/" +
"testJson.json",jsonStr);
}
}
三、LitJson的使用
3.1.读取json
private void ReadJson()
{
string readjsondata = File.ReadAllText(Application.persistentDataPath+"/"+"Cherry2.json");
Cherry cherry2 = JsonMapper.ToObject<Cherry>(readjsondata);
Debug.Log(readjsondata);
}
3.2写入json
using LitJson;
public class Student2
{
public int age;
public string name;
public Student2(){}
public Student2(int age,string name)
{
this.age = age;
this.name = name;
}
}
public class Cherry
{
public string name;
public int age;
public bool Sex;
public float testF;
public double testD;
public int[] ids;
public List<int> ids2;
//public Dictionary<int, string> dic;
public Dictionary<string, string> dic2;
public Student2 s1;
public List<Student2> s2s;
private int privateI = 1;
protected int protectedI = 2;
}
public class LitJsonTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Cherry cherry = new Cherry();
cherry.name = "cherry";
cherry.age = 24;
cherry.Sex = false;
cherry.testF = 1.2f;
cherry.testD = 1.3d;
cherry.ids=new int[] {1,2,3,4};
cherry.ids2 = new List<int> { 5,6,7,8};
//cherry.dic=new Dictionary<int, string>() { { 1, "2, 3, 4" } };
cherry.dic2 = new Dictionary<string, string>() { { "1","123"},{ "2","234"} };
cherry.s1 = new Student2(1,"yan");
cherry.s2s = new List<Student2>() { new Student2(2, "啊"), new Student2(3, "haha") };
SendJson(cherry);
ReadJson();
}
private void SendJson(Cherry cherry)
{
string jsondata = JsonMapper.ToJson(cherry);
Debug.Log(jsondata);
File.WriteAllText(Application.persistentDataPath + "/" + "Cherry2.json", jsondata);
Debug.Log(Application.persistentDataPath);
}
}