在unity的开发中,经常会用到Json来存储数据文件。如下我将带领大家来实现一套json文件的存储系统。功能如下:
1.若没有json文件,将创建一个json文件,并赋值为数据类StudentData中的数据;
2.若存在json文件,将读取json文件中的数据,并赋值给数据类StudentData;
3.实现对json文件的增,删,改,查。
(注意:本代码使用LitJson.dll进行解析,需把LitJson.dll放入到Unity的Assets中的任意文件夹中 Json官网:http://www.json.org)
代码如下:
学生数据类StudentData
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//性别枚举
public enum SexType{
None,Female,Male
}
[System.Serializable]
//课程分数
public class Grade{
public float math;
public float physics;
public float english;
}
[System.Serializable]
//学生类
public class StudentData {
//学号
public float id;
//学号
public string name;
//性别
public SexType sexType=SexType.None;
//年龄
public int age;
//是否统考
public bool bCylet;
//成绩
public Grade grade;
//存储的文件名
public const string FILENAME="StudentData";
public StudentData(float id,string name,SexType sexType,int age,bool bCylet,Grade grade){
this.id = id;
this.name = name;
this.sexType = sexType;
this.age = age;
this.bCylet = bCylet;
this.grade = grade;
}
}
Json文件管理
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System;
using LitJson;
public class JsonMgr : MonoBehaviour {
public static JsonMgr _instance;
//学生数据类
public StudentData studentData;
void Awke()
{
_instance = this;
}
void Start()
{
if (File.Exists(Application.dataPath + "//" + StudentData.FILENAME)) {
studentData = ParseFile (Application.dataPath, StudentData.FILENAME);
}
else
{
CreateFile(Application.dataPath, StudentData.FILENAME, studentData);
}
}
//创建json文件
void CreateFile(string filePath,string fileName,StudentData studentData)
{
StringBuilder stringBuilder = new StringBuilder();
JsonWriter jsonWriter = new JsonWriter(stringBuilder);
jsonWriter.WriteObjectStart();
jsonWriter.WritePropertyName("id");
jsonWriter.Write(studentData.id);
jsonWriter.WritePropertyName("name");
jsonWriter.Write(studentData.name);
jsonWriter.WritePropertyName("sexType");
jsonWriter.Write(studentData.sexType.ToString());
jsonWriter.WritePropertyName("age");
jsonWriter.Write(studentData.age);
jsonWriter.WritePropertyName("bCylet");
jsonWriter.Write(studentData.bCylet);
jsonWriter.WritePropertyName ("grade");
jsonWriter.WriteArrayStart ();
jsonWriter.WriteObjectStart ();
jsonWriter.WritePropertyName ("math");
jsonWriter.Write (studentData.grade.math);
jsonWriter.WriteObjectEnd ();
jsonWriter.WriteObjectStart ();
jsonWriter.WritePropertyName ("physics");
jsonWriter.Write (studentData.grade.physics);
jsonWriter.WriteObjectEnd ();
jsonWriter.WriteObjectStart ();
jsonWriter.WritePropertyName ("english");
jsonWriter.Write (studentData.grade.english);
jsonWriter.WriteObjectEnd ();
jsonWriter.WriteArrayEnd();
jsonWriter.WriteObjectEnd();
FileStream fileStream = new FileStream(filePath + "//" + fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.WriteLine(stringBuilder.ToString());
streamWriter.Close();
fileStream.Close();
fileStream.Dispose();
}
//解析json文件
private StudentData ParseFile(string filePath, string fileName)
{
StudentData studentData = null;
if (File.Exists(filePath + "//" + fileName))
{
float id;
string name;
SexType sexType=SexType.None;
int age;
bool bCylet;
Grade grade=new Grade();
FileStream fileStream = new FileStream(filePath + "//" + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader streamReader = new StreamReader(fileStream);
string strData = streamReader.ReadToEnd ();
JsonData jsonData = JsonMapper.ToObject(strData);
id = float.Parse (jsonData ["id"].ToString ());
name = jsonData["name"].ToString();
sexType = (SexType)Enum.Parse (typeof(SexType), jsonData ["sexType"].ToString ());
age = int.Parse (jsonData ["age"].ToString());
bCylet = bool.Parse (jsonData ["bCylet"].ToString());
grade.math = float.Parse(jsonData ["grade"] [0] ["math"].ToString ());
grade.physics= float.Parse(jsonData ["grade"] [1] ["physics"].ToString ());
grade.english= float.Parse(jsonData ["grade"] [2] ["english"].ToString ());
studentData =new StudentData(id,name,sexType,age,bCylet,grade);
}
return studentData;
}
void OnApplicationQuit()
{
CreateFile(Application.dataPath, StudentData.FILENAME,studentData);
}
}
生成的Json文件
{
"id": 1303070223,
"name": "zhangsan",
"sexType": "Male",
"age": 23,
"bCylet": true,
"grade": [
{
"math": 96
},
{
"physics": 99
},
{
"english": null
}
]
}