unity自带解析json的方法:
JsonUtility.FromJson<JsonInfoList>(string s);
string s = JsonUtility.ToJson(jsonInfoList);
JsonInfoList 需要是个对象:
class JsonInfoList
{
public List<UIPanelDAL> infoList = null;
}
然后看一下JSON文件:
<UIPanelType.json>
{ //这个符号是对象的意思,解析出来用对象接收
"infoList": //对应上面class中的变量名称infoList
[ //这个符号是list的意思,list中存了好多对象
{
"panelTypeString": "MainMenu",
"path": "UIPanel/MainMenuPanel"
},
{
"panelTypeString": "ItemMessage",
"path": "UIPanel/ItemMessagePanel"
},
{
"panelTypeString": "Knapsack",
"path": "UIPanel/KnapsackPanel"
},
{
"panelTypeString": "Shop",
"path": "UIPanel/ShopPanel"
},
{
"panelTypeString": "Skill",
"path": "UIPanel/SkillPanel"
},
{
"panelTypeString": "System",
"path": "UIPanel/SystemPanel"
},
{
"panelTypeString": "Task",
"path": "UIPanel/TaskPanel"
}
]
}
2.UIPanelDAL序列化
using UnityEngine;
using System;
[Serializable]
public class UIPanelDAL : ISerializationCallbackReceiver {
[NonSerialized]
public UIPanelType panelType;
public string panelTypeString;
public string path;
public void OnAfterDeserialize()
{
//Debug.Log("panelTypeString " + panelTypeString);
panelType = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
}
public void OnBeforeSerialize()
{
}
}