
【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili
网站 Json.cn 用于Json的检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Json;
public class JsonGenerate : MonoBehaviour {
string json = "{'name':'xiaoming','age':18}";
private void Awake()
{
}
private void Start()
{
JsonObject obj = new JsonObject();
//存储位置
obj.Add("position",transform.position.ToString());
//存储颜色
obj.Add("color", GetComponent().material.color.ToString());
//打印
Debug.Log(obj.ToString());//批量添加
KeyValuePair[] kvs;
kvs = new KeyValuePair[3];
kvs[0] = new KeyValuePair("IsKinematic", true);
kvs[1] = new KeyValuePair("UseGravity", false);
kvs[2] = new KeyValuePair("IsTrigger", false);
obj.AddRange(kvs);
}private void JsonBase()
{
//创建Json对象,相当于一个{}
JsonObject person_Obj = new JsonObject();
//向大括号中添加键值对
person_Obj.Add("name","xiaoming");
person_Obj.Add("age",18);
//创建json数组,相当于一个中括号
JsonArray scores = new JsonArray();
//向数组中添加元素
scores.AddRange(90,"缺考","优秀");
//语文成绩
JsonObject chineseScore = new JsonObject();
chineseScore.Add("语文", 75);
scores.Add(chineseScore);
//添加数组元素
person_Obj.Add("scores",scores);
//打印Json
Debug.Log(person_Obj.ToString());
}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;public class JsonParse : MonoBehaviour {
string json = "{'name':'xiaoming','age':18}";
private void Start()
{
//解析Json
JsonData data=JsonMapper.ToObject(json);
//打印结果
Debug.Log(data["name"]);
//Debug.Log(data["技能"][0]["技能名称"]);
//for (int i = 0; i < data["技能"].Count; i++) {
// Debug.Log(data["技能"][i]["技能名称"]);
//}
}
}