内置的JsonUtility解析转为字符串的json会出现反编译失败问题,可能跟版本有关
解决方法:
- 不用JsonUtility
- unity到(Package Manager)资源商店安装Newtonsoft.Json
- 在c#新建类Readjson
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Newtonsoft.Json; //引用库
//json数据定义
//序列化标签(建议写上)
[Serializable]
public class Readjson : MonoBehaviour
{
//显示开关
public bool _switch;
//报警状态,0绿 1红
public int _state;
//对象名字
public string _objname;
//解析字符串
public Readjson GetjsonValue(string jsonstring)
{
Readjson Value = JsonConvert.DeserializeObject<Readjson>(jsonstring);
//将结果返回
return Value;
}
}
- 使用新建Readjson类,以下是案例(记得在周期函数调用jsonvalue)
public void jsonvalue(string jsonString)
{
//使用解析方法,myvalue接收结果
Readjson myvalue = readjson.GetjsonValue(jsonString);
//打印myvalue的结果
Debug.Log(myvalue._switch);
Debug.Log(myvalue._state);
}
- jsonString 模拟json字符串
"{\"_objname\":\"John Doe\",\"_state\":30,\"_switch\":true}"