Json简介
- JSON 是纯⽂本
- JSON 是⼀种轻量级的数据交换格式
- JSON 具有层级结构(值中存在值)
Json语法
- 数据都放置到⼀个⼤括号⾥
- ⼀对对的数据放到⼤括号⾥
- ⼀个个的数据放到中括号⾥
- ⼀个⼤括号或⼀个中括号都可以表示⼀个值
- 数据与数据之间⽤逗号分隔
- Key必须要⽤引号引起来
- Value如果是字符串要⽤引号引起来
- 最后⼀个数据后⾯不要加逗号
例子
{
"students":[ //students是key
{ //之后在 [ ] 中的都是此key的Value
"name":"小明", //name是key 小明 是value
"num":19, //同级的用 " , "隔开
"sex":"M" //最后的数据后方不要加 " , "
},
{
"name":"小红",
"num":11,
"sex":”W”
}
]
}
Json在Unity中的使用
首先需要在Unity项目中导入两个库System.Json和LitJson 下载链接
这两个文件都需要放在Plugins文件夹中
- Plugins(DLL 动态链接库)
- Editor(插件脚本)
- Standard Assets (标准资源包)
这三个都是提前编译的⽂件夹
Json的类
- JsonObject 是一个无序的零个或更多的键值对的集合
- JsonArray 是零个或更多的JsonValue 对象的有序序列
- JsonValue 具体的一个Value值
JsonObject
- Add(string,JsonValue) 添加⼀个键值对
- AddRange() 添加多个键值对
- Remove 移除某个键值对
- Clear 清空
JsonArray
- Add(JsonValue) 添加⼀个数组元素
- AddRange() 添加多个数组元素
JsonValue
他可以表示的数据类型有:bool、int、float、string、jsonObject、jsonArray
例子
生成Json
using System.Json;
public class JsonGenerate
{
//创建⼀个JSON对象,相当于⼀个⼤括号
JsonObject jsonTransform = new JsonObject ();
//创建⼀个JSON值对象,存储⼀个Value
JsonValue jsonPosition = "10,20,30";
//JSON对象,也就是⼤括号,添加⼀个key:value
jsonTransform.Add(“position”,jsonPosition);
//定义⼀个值数组,⽤来存储,中括号中的⼀个个的值
JsonValue[] rotationXYZ = new JsonValue[] {20,30,40} ;
//将这个数组中的⼀个个的值放到JsonArray数组对象中
JsonArray jsonRotation = new JsonArray(rotationXYZ);
//⼀个JsonArray对象,也可以是⼀个Value,
//所以可以⽤jsonTransform来Add
jsonTransform.Add(“rotation”,jsonRotation);
JsonObject x = new JsonObject ();
x.Add(“x”,10);
JsonObject y = new JsonObject ();
y.Add(“y”,20);
JsonObject z = new JsonObject ();
z.Add(“z”,30);
//实例⼀个jsonValues数组(所有类型都可以转换成jsonValue)
JsonValue[] scaleXYZ = new JsonValue[] {x,y,z};
//将实例好了的数组,添加到jsonArray对象中,形成⼀个jsonArray对象
//作⽤在于给这个整体,添加⼀个中括号[]
JsonArray jsonScale = new JsonArray (scaleXYZ);
//将这个整体,放⼊到jsonTransform中
jsonTransform.Add(“scale”,jsonScale);
}
方法二
使用LitJson生成
public void func2()
{
JsonData heros = new JsonData(); //最外界的大括号
JsonData hero = new JsonData(); //集合 相当于 []
JsonData hero1 = new JsonData();
JsonData hero2 = new JsonData();
//JsonType是个枚举类,用来设置数据类型
hero.SetJsonType(JsonType.Array);
hero1["name"] = "德玛西亚";
hero1["hp"] = 1300;
hero2["name"] = "龙龟";
hero2["hp"] = 1800;
heros["heros"] = hero;
}
结果
{
“position”:”10,20,30”,
”rotation”:[20,30,40],
”scale”:[
{“x”:10},
{“y”:20},
{“z”:30}
]
}
解析Json
Json的解析用的是LitJson提供的方法
- 把对象转化成JSON格式字符串: JsonMapper.ToJson
- 把JSON格式字符串转化成对象: JsonMapper.ToObject
- 把JSON格式字符串转化成指定对象: JsonMapper.ToObject <>
using System.Json;
public class JsonGenerate
{
//解析Json,先将JSON格式的字符串转化成对象
JsonData data = JsonMapper.ToObject (JsonString);
//获取数据
//pos的值为 "10,20,30"
string pos = data ["position"];
//将rotation中的3个值存入rot数组中
int[] rot = new int[3];
foreach(int i = 0; i < data ["rotation"].Count; i++)
{
rot[i] = data ["rotation"] [i];
}
}
内置类
using UnityEngine;
using System;
[Serializable]
public class Person
{
public string name;
public int age;
}
[Serializable]
public class Persons
{
public Person[] per;
}
public class JsonUtilityDemo : MonoBehaviour {
public Person person;
public Person person2;
public Persons persons;
void Start () {
person = new Person();
person2 = new Person();
person.name = "xiaoming";
person.age = 15;
person2.name = "hong";
person2.age = 15;
//生成
persons.per = new Person[] { person, person2 };
string ss = JsonUtility.ToJson(persons);
//解析
Persons pers = JsonUtility.FromJson<Persons>(ss);
Debug.Log(pers.per[0].name);
}
}