//序列化类
[Serializable]
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[NonSerialized]
//非序列化字段,无法被获取
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
public Person(int id, string name)
{
Id = id;
Name = name;
}
public Person(int id, string name, string sex)
{
Id = id;
Name = name;
Sex = sex;
}
}
JSON的四种用法:
void GetJSON()
{
//文件的位置
string path = Application.dataPath +"/flod/hh.txt";
if (File.Exists(path))
{
try
{
//文件的读取,JsonData读取的方式,JsonMapper.ToObject()方法:把JSON字符串转成对象
JsonData j = JsonMapper.ToObject(File.ReadAllText(path));
foreach (JsonData item in j)//遍历JSON文件
{
int id = int.Parse(item["Id"].ToString());
string name = item["Name"].ToString();
print(id + "\t" + name);
}
}
catch (Exception e)
{
print(e.Message);
}
}
else
{
File.Create(path);//文件不存在的情况下创建文件
}
}
void GetJSON2()
{
string path = Application.dataPath + "/flod/hh.txt";
if (File.Exists(path))
{
//JsonMapper.ToObject()方法:把JSON字符串转成数组类型
Skill[] skills = JsonMapper.ToObject<Skill[]>(File.ReadAllText(path));
foreach (var person in skills)
{
print(person.Id + "\t" + person.Name);
}
}
else
{
File.Create(path);//文件不存在的情况下创建文件
}
}
void GetJSON3()
{
string path = Application.dataPath + "/flod/hh.txt";
if (File.Exists(path))
{
//JsonMapper.ToObject()方法:把JSON字符串转成集合类型
List<Skill> skills = JsonMapper.ToObject<List<Skill>>(File.ReadAllText(path));
foreach (var person in skills)
{
print(person.Id + "\t" + person.Name);
}
}
else
{
File.Create(path);//文件不存在的情况下创建文件
}
}
void GetJSON4()
{
string path = Application.dataPath + "/flod/hh.txt";
if (File.Exists(path))
{
//JsonMapper.ToObject()方法:把JSON字符串转成One类类型
One one = JsonMapper.ToObject<One>(File.ReadAllText(path));
print(one.Name);
foreach (var person in one.listSkill)
{
print(person.Id + "\t" + person.Name);
}
}
else
{
File.Create(path);//文件不存在的情况下创建文件
}
}
[Serializable]
public class One
{
public string Name{ get; set; }
public List<Skill> listSkill{get;set;}
}
public class Skill
{
public int Id { get; set; }
public string Name { get; set; }
}
JSON文件内容
[
{
"Id":101,
"Name":"天才少年"
}
]
用JSON写注册登录,注册写入,登录读取。
代码如下:
类:Players
[Serializable]
public class Players
{
public string Id;
public string Password;
public List<Players> list;
public Players()
{
}
public Players(string id,string password)
{
Id= id;
Password= password;
}
}
[Serializable]
public class HomeWork3_13 : MonoBehaviour
{
private InputField names, password;
private Button register, login;
Players p1;//比对注册用户是否重复
Players p2;//比对登录用户,是否存在
public void Re()
{
//JSON文件路径
string path = Application.dataPath + "/JSON/player.json";
//读取JSON里面所有的内容
string[] st = File.ReadAllLines(path);
//判断这个文件是否文件存在
if (File.Exists(path))
{
try
{
//通过构造函数把输入内容放入JSON
Players p = new Players(names.text,password.text);
//把JSON一个个放入集合内
p.list = new List<Players>();
for (int i = 0; i < st.Length; i++)//遍历Json里面所有的内容
{
p1 = JsonUtility.FromJson<Players>(st[i]);
}
//比对下面条件是否满足
if (names.text==p1.Id)
{
print("账号重复,请重试");
names.text = null;
password.text = null;
}
else if (names.text != p1.Id)
{
print("注册成功");
p.list.Add(p);//数据加入集合
string s = JsonUtility.ToJson(p) + "\n";//序列化(把数据传入JSON文件)
File.AppendAllText(path, s);
names.text = null;
password.text = null;
}
}
catch (Exception e)//异常检测
{
print(e.Message);
}
}
else
{
File.Create(path);//如果没有这个文件将会生成这个文件
}
}
private void lo()
{
string path = Application.dataPath + "/JSON/player.json";
string[] st = File.ReadAllLines(path);//读取文件
for (int i = 0; i < st.Length; i++)
{
p2 = JsonUtility.FromJson<Players>(st[i]);//反序列化(解析JSON文件)
}
if (names.text == p2.Id && password.text == p2.Password)
{
print("登录成功");
names.text = null;
password.text = null;
}
else
{
print("登录失败");
names.text = null;
password.text = null;
}
}
void Start()
{
names=GameObject.Find("Uname").GetComponent<InputField>();
password = GameObject.Find("Upassword").GetComponent<InputField>();
register = GameObject.Find("Register").GetComponent<Button>();
register.onClick.AddListener(Re);
login= GameObject.Find("Login").GetComponent<Button>();
login.onClick.AddListener(lo);
}
void Update()
{
}
}
用JSON去存放技能
TXT为转载类,因为这个和上面的内容是同时写的,出现了一点小问题所以这里我用了一个TXT类去接收一下技能的存放
public class TXT
{
public Players[] player;
}
public class BagsJson : MonoBehaviour
{
void Start()
{
Skill();
}
void Skill()
{
//JSON文件位置
string path = Application.dataPath + "/JSON/play.json";
//判断是否有该文件
if (!File.Exists(path))
{
File.Create(path);//没有该文件就创建文件
}
else
{
//JSON的技能存放
Players player1 = new Players();
player1.Id = "技能1:";
player1.Password = "超级无敌大雪球";
Players player2 = new Players();
player1.Id = "技能2:";
player1.Password = "超级无敌风火轮";
//存放技能
Players[] player3 = new Players[] { player1, player2 };
//因为Players类的原因,这里需要一个TXT转载类去接收一下该类的内容
TXT t =new TXT();
t.player = player3;
//对象转成JSON类型
string s = JsonUtility.ToJson(t) + "\n";
//添加对象
File.AppendAllText(path, s);
}
}
void Update()
{
}
}