当前位置:   article > 正文

Unity存档--Json_unity jsondata文件

unity jsondata文件
  1. 认识Json:Json是一门数据描述语言,它只是一种数据的格式,单机游戏一般是通过写Json文件操作,也就是写文件的方式存在本地电脑中,联网游戏一般就是将它放在数据库中

  1. 了解Json的书写格式:假比现在有人物

名字:李逍遥 年龄:18 武功:万雷诀,

名字:赵灵儿 年龄:14 武功:御剑术

这两个人物分别用Json的格式表达出来就是:

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

这里我们来看一个c#的类

class Person

{

public string Name;

public string Age;

public string Kungfu;

}

Person p1=new Person();

p1.Name="李逍遥";

p1.Age=18;

p1.Kungfu="万雷诀";

这时可以看出p1这个对象就可以用Json这种格式储存下来

Json格式不仅可以描述类的对象({}),还可以描述数组([])

比如[1,2,3,4,5]这在c#中就是int[]数组,["李逍遥","赵灵儿"]这在c#中就是string[]数组

复杂一点的例如:

[

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

]

这个其实就是[{},{}]是这种格式,也就可以看出在c#中是存有Person对象的数组,也就是Person[]

再比如这样:

{

“personArry”:

[

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

]

}

这个看着挺复杂的但是看它的结构是这样的{ [{},{}] },{}可以理解为类的对象,[]理解为数组,那么这里再c#中理解为,有两个类,最外层的{}是一个类,这个类中有一个名叫personArry数组,这个personArry数组是用来存放Person这个类的对象的,且存放了两个对象,就像这样

class Persons(类名)

{

Public Person personArry[];

}

了解了Json的基本格式后,就学习如何在unity中使用,目前有两种方法使用,第一种就是JsonUtility,第二种就是LitJson

在使用Json都会涉及到两个过程,一个创建,一个解析

首先先创建了空对象,在空对象上挂载了测试JsonUtility的脚本,后面的LitJson同理

  1. JsonUnility:

(1)创建Json

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;//想要使用序列化就需要引入这个命名空间
  5. [Serializable]//需要在转换为json格式的类的上方添加序列化
  6. public class Person
  7. {
  8. public string name;
  9. public int age;
  10. }
  11. public class JsonUtilityDemo : MonoBehaviour
  12. {
  13. void Start()
  14. {
  15. Person person = new Person();
  16. person.name = "李逍遥";
  17. person.age = 18;
  18. //转成json字符串
  19. string jsonStr = JsonUtility.ToJson(person);
  20. Debug.Log(jsonStr);
  21. }
  22. }

控制台输出的结果为:

再做复杂一点的结构比如{ [{},{}] }

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. [Serializable]
  6. public class Person
  7. {
  8. public string name;
  9. public int age;
  10. }
  11. [Serializable]
  12. public class Persons
  13. {
  14. public Person[] personArry;
  15. }
  16. public class JsonUtilityDemo : MonoBehaviour
  17. {
  18. void Start()
  19. {
  20. Person person = new Person();
  21. person.name = "李逍遥";
  22. person.age = 18;
  23. //转成json字符串
  24. string jsonStr = JsonUtility.ToJson(person);
  25. Person person2 = new Person();
  26. person2.name = "赵灵儿";
  27. person2.age = 14;
  28. Persons persons = new Persons();
  29. persons.personArry = new Person[] { person, person2 };
  30. string jsonstr2 = JsonUtility.ToJson(persons);
  31. Debug.Log(jsonstr2);
  32. }
  33. }

(2)解析Json:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. [Serializable]
  6. public class Person
  7. {
  8. public string name;
  9. public int age;
  10. }
  11. [Serializable]
  12. public class Persons
  13. {
  14. public Person[] personArry;
  15. }
  16. public class JsonUtilityDemo : MonoBehaviour
  17. {
  18. void Start()
  19. {
  20. Person person = new Person();
  21. person.name = "李逍遥";
  22. person.age = 18;
  23. //转成json字符串
  24. string jsonStr = JsonUtility.ToJson(person);
  25. Person person2 = new Person();
  26. person2.name = "赵灵儿";
  27. person2.age = 14;
  28. Persons persons = new Persons();
  29. persons.personArry = new Person[] { person, person2 };
  30. string jsonstr2 = JsonUtility.ToJson(persons);
  31. //Debug.Log(jsonstr2);
  32. //解析
  33. Persons ps = JsonUtility.FromJson<Persons>(jsonstr2);//这里的类是依据最外层{}决定的
  34. foreach(Person p in ps.personArry)
  35. {
  36. Debug.Log(p.name);
  37. }
  38. }
  39. }

  1. LitJson:LitJson的创建和解析有两种方案

因为LitJson是第三方插件,需要自己下载litjson的dll文件,并最好在Assets中创建名叫Plugins的文件夹,并把litjson的文件拖过去即可。

方案(一):(1)创建Json

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;//记得引入litjson的命名空间
  5. public class Hero
  6. {
  7. public string name;
  8. public int age;
  9. }
  10. public class Heros
  11. {
  12. public Hero[] HeroArry;
  13. }
  14. public class litJson : MonoBehaviour
  15. {
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. func1();
  20. }
  21. //第一种创建和解析的方法
  22. void func1()
  23. {
  24. Hero hero1 = new Hero();
  25. hero1.name = "超人";
  26. hero1.age = 23;
  27. Hero hero2 = new Hero();
  28. hero2.name = "蝙蝠侠";
  29. hero2.age = 42;
  30. Heros heros = new Heros();
  31. heros.HeroArry = new Hero[] { hero1, hero2 };
  32. //创建Json
  33. string jsonStr = JsonMapper.ToJson(heros);
  34. //这样的结构就是{ [{},{}] }
  35. Debug.Log(jsonStr);
  36. }
  37. }

(2)解析Json

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;//记得引入litjson的命名空间
  5. public class Hero
  6. {
  7. public string name;
  8. public int age;
  9. }
  10. public class Heros
  11. {
  12. public Hero[] HeroArry;
  13. }
  14. public class litJson : MonoBehaviour
  15. {
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. func1();
  20. }
  21. //第一种创建和解析的方法
  22. void func1()
  23. {
  24. Hero hero1 = new Hero();
  25. hero1.name = "超人";
  26. hero1.age = 23;
  27. Hero hero2 = new Hero();
  28. hero2.name = "蝙蝠侠";
  29. hero2.age = 42;
  30. Heros heros = new Heros();
  31. heros.HeroArry = new Hero[] { hero1, hero2 };
  32. //创建Json
  33. string jsonStr = JsonMapper.ToJson(heros);
  34. //这样的结构就是{ [{},{}] }
  35. //Debug.Log(jsonStr);
  36. //解析Json
  37. Heros hs = JsonMapper.ToObject<Heros>(jsonStr);
  38. Debug.Log(hs.HeroArry[0].name);
  39. }
  40. }

方案(二):(1)创建Json

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;//记得引入litjson的命名空间
  5. public class Hero
  6. {
  7. public string name;
  8. public int age;
  9. }
  10. public class Heros
  11. {
  12. public Hero[] HeroArry;
  13. }
  14. public class litJson : MonoBehaviour
  15. {
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. func2();
  20. }
  21. //第二种方案创建Json
  22. void func2()//也可以不用创建类来创建Json
  23. {
  24. //{"name":"超人","power";23}
  25. JsonData jd = new JsonData();//这个JsonData可以代表类{}或者数组[]
  26. //jd.SetJsonType(JsonType.Object);//可以像这样设置JsonData的类型,不过也可以直接使用,例如
  27. jd["name"] = "超人";
  28. jd["power"] = 90;
  29. Debug.Log(jd.ToJson());
  30. }
  31. }

创建更复杂的结构{ [{},{}] }

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;//记得引入litjson的命名空间
  5. public class Hero
  6. {
  7. public string name;
  8. public int age;
  9. }
  10. public class Heros
  11. {
  12. public Hero[] HeroArry;
  13. }
  14. public class litJson : MonoBehaviour
  15. {
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. func2();
  20. }
  21. //第二种方案创建Json
  22. void func2()//也可以不用创建类来创建Json
  23. {
  24. //{"name":"超人","power";23}
  25. //JsonData jd = new JsonData();//这个JsonData可以代表类{}或者数组[]
  26. //jd.SetJsonType(JsonType.Object);//可以像这样设置JsonData的类型,不过也可以直接使用,例如
  27. // jd["name"] = "超人";
  28. // jd["power"] = 90;
  29. //Debug.Log(jd.ToJson());
  30. //这样的结构{[{},{}]}
  31. JsonData herosJD = new JsonData();//这是最外层的{}
  32. JsonData hero1JD = new JsonData();//这是中间的两个{}
  33. hero1JD["name"] = "超人";
  34. hero1JD["power"] = 90;
  35. JsonData hero2JD = new JsonData();
  36. hero2JD["name"] = "蝙蝠侠";
  37. hero2JD["power"] = 45;
  38. JsonData heroArry = new JsonData();//这个是[]
  39. heroArry.Add(hero1JD);//这是将两个{}对象放在数组中
  40. heroArry.Add(hero2JD);
  41. herosJD["heroArry"]= heroArry;//这一步将数组放在最外层的{}中
  42. Debug.Log(herosJD.ToJson());
  43. }
  44. }

(2)解析Json

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;//记得引入litjson的命名空间
  5. public class Hero
  6. {
  7. public string name;
  8. public int age;
  9. }
  10. public class Heros
  11. {
  12. public Hero[] HeroArry;
  13. }
  14. public class litJson : MonoBehaviour
  15. {
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. func3();
  20. }
  21.     //第二种方案解析Json
  22. void func3()
  23. {
  24. string jsonStr = "{'heroArry':[{'name':'超人','power':90},{'name':'蝙蝠侠','power':53}]}";
  25. JsonData herosJD = JsonMapper.ToObject(jsonStr);//最外层的{}
  26. JsonData heroArry = herosJD["heroArry"];//[]
  27. foreach(JsonData heroJD in heroArry)
  28. {
  29. Debug.Log(heroJD["name"].ToString());
  30. }
  31. }
  32. }

创建Json就是类转换为Json的过程,解析Json就是将Json转换为类的过程

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/794134
推荐阅读
相关标签
  

闽ICP备14008679号