目录
1.Untiy中使用第三方类库LitJSON创建和解析JSON过程如下:
2.VS中使用第三方类库LitJSON创建和解析JSON过程(json为数组时)
3.VS中使用第三方类库LitJSON创建和解析JSON过程(json为对象时)
4.json在线编辑器(http://www.bejson.com/)
JSON和C#是可以相互转换的,在JSON中,{}表示对象,[]表示数组
JSON中的[1,2,3,4]对应C#中的int[1,2,3,4],
JSON中的["张三","李四","王五"]对应C#中的string["张三","李四","王五"],
[
{"name":"张三","age":18,"sex":"男"},
{"name":"李四","age":20,"sex":"女"},
{"name":"王五","age":22,"sex":"男"}
]
对应C#中的person对象,即:Person[]
JSON有两种解析方法,一种是第三方类库LitJSON(推荐),另一种是unity自带的JsonUtility(我在实际操作中出了一点Bug,故暂时不做深入研究)。
两种引入litjson的方法:
1,去litjson的网站下载litjson.dll 然后添加引用 找到dll所在目录
2,右键引用 打开管理netget程序包,在联机里面搜索litjson 在搜索结果中选择一个 点击安装
1.Untiy中使用第三方类库LitJSON创建和解析JSON过程如下:
litjson下载:https://pan.baidu.com/s/11izRvtjXqzS4tNgxP75qjQ
首先导入LitJson.dll,并将其放入Plugins文件夹里(一定要是Plugins才行),然后就可以在vs里导入LitJson命名空间了
using UnityEngine;
using LitJson;
public class Person
{
public string name;
public int age;
}
public class Persons
{
public Person[] persons;
}
public class LitJSONDemo : MonoBehaviour {
void Start () {
Person p1 = new Person() ;
p1.name = "张三";
p1.age = 18;
Person p2 = new Person();
p2.name = "李四";
p2.age = 20;
Persons persons = new Persons();
persons.persons = new Person[] { p1, p2 };
//创建JSON
string jsonStr = JsonMapper.ToJson(persons);
Debug.Log(jsonStr);
//解析JSON
Persons newPersons = JsonMapper.ToObject<Persons>(jsonStr);
Debug.Log(newPersons.persons[1].name);
}
}
第二种:(常用)
using UnityEngine;
using LitJson;
public class LitJSONDemo : MonoBehaviour {
void Start () {
string jsonStr = "{'persons':[{'name':'张三','age':18},{'name':'李四','age':20}]}";
JsonData personsJd = JsonMapper.ToObject(jsonStr);
JsonData p = personsJd["persons"];
Debug.Log(p[0]["name"]+","+p [0]["age"]+" "+ p[1]["name"] + "," + p[1]["age"]);
foreach (JsonData person in p)
{
Debug.Log(person["name"].ToString()+","+ (int)person["age"]);
}
}
}
2.VS中使用第三方类库LitJSON创建和解析JSON过程(json为数组时)
1)利用LitJSON解析json文本
json技能信息.txt的属性:
json技能信息.txt在保存时记得将编码选为utf-8,以防不识别中文
json技能信息.txt:
[
{"id":2,"name":"天下无双","damage":123 },
{"id":3,"name":"天下无贼","damage":21 },
{"id":4,"name":"咫尺天涯","damage":900 }
]
Skill.txt:
class Skill
{
public int id;
public int damage;
public string name;
public override string ToString()
{
return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);
}
}
Program.cs:
class Program
{
static void Main(string[] args)
{
List<Skill> skillList = new List<Skill>();
//我们使用jsonMapper去解析json文本
//jsondata代表一个数组或者一个对象
//在这里jsonData代表数组
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json技能信息.txt"));
foreach (JsonData temp in jsonData)//在这里temp代表一个对象
{
Skill skill = new Skill();
JsonData idValue = temp["id"]; //通过字符串索引器可以取得键值对的值
JsonData nameValue = temp["name"];
JsonData damageValue = temp["damage"];
int id = Int32.Parse(idValue.ToString());
int damage = Int32.Parse(damageValue.ToString());
//Console.WriteLine(id+":"+nameValue.ToString()+":"+damage);
skill.id = id;
skill.damage = damage;
skill.name = nameValue.ToString();
skillList.Add(skill);
}
foreach (var temp in skillList)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
2)利用泛型解析json文本(更简单)
(1)使用数组方式
json技能信息.txt:
[
{"id":2,"name":"天下无双","damage":123 },
{"id":3,"name":"天下无贼","damage":21 },
{"id":4,"name":"咫尺天涯","damage":900 }
]
Skill.txt:
class Skill
{
public int id;
public int damage;
public string name;
public override string ToString()
{
return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);
}
}
Program.cs:
class Program
{
static void Main(string[] args)
{
List<Skill> skillList = new List<Skill>();
//使用泛型去解析json
//json里面对象的键必须跟定义的类里面的字段或者属性保持一致
Skill[] skillArray = JsonMapper.ToObject<Skill[]>(File.ReadAllText("json技能信息.txt"));
foreach (var temp in skillArray)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
(2)使用集合方式(只要能使用数组的地方都可以使用集合)
json技能信息.txt:
[
{"id":2,"name":"天下无双","damage":123 },
{"id":3,"name":"天下无贼","damage":21 },
{"id":4,"name":"咫尺天涯","damage":900 }
]
Skill.txt:
class Skill
{
public int id;
public int damage;
public string name;
public override string ToString()
{
return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);
}
}
Program.cs:
class Program
{
static void Main(string[] args)
{
List<Skill> skillList = JsonMapper.ToObject<List<Skill>>(File.ReadAllText("json技能信息.txt"));
foreach (var temp in skillList)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
3.VS中使用第三方类库LitJSON创建和解析JSON过程(json为对象时)
主角信息.txt:
{
"Name":"siki",
"Level":99,
"Age":18,
"SkillList":[
{"Id":2,"Name":"天下无双","Damage":123 },
{"Id":3,"Name":"天下无贼","Damage":21 },
{"Id":4,"Name":"咫尺天涯","Damage":900 }
]
}
Skills:
class Skill
{
public int Id { get; set; }
public int Damage { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Damage: {1}, Name: {2}", Id, Damage, Name);
}
}
Player.cs:
class Player
{
public string Name { get; set; }
public int Level { get; set; }
public int Age { get; set; }
public List<Skill> SkillList { get; set; }
public override string ToString()
{
return string.Format("Name: {0}, Level: {1}, Age: {2}, SkillList: {3}", Name, Level, Age, SkillList);
}
}
Program.cs:
class Program
{
static void Main(string[] args)
{
Player p = JsonMapper.ToObject<Player>(File.ReadAllText("主角信息.txt"));
Console.WriteLine(p);
foreach (var temp in p.SkillList)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}