介绍
使用Unity自身的JsonUtility解析字典
- 字典类型实现接口ISerializationCallbackReceiver,相当于在转换之间进行键值对列表的分开解析
Json
{
"KeyList": [1, 2],
"ValueList": [{
"panelTypeString": "1",
"path": "q"
}, {
"panelTypeString": "2",
"path": "w"
}]
}
代码
using System;
using System.Collections.Generic;
using UnityEngine;
public class JsonLoad : MonoBehaviour
{
void Start()
{
LoadJson();
}
private void LoadJson()
{
string str = Resources.Load<TextAsset>("Json").text;
UIDataJson json = JsonUtility.FromJson<UIDataJson>(str);
Dictionary<int, UIData> dic = json.infodic;
foreach (var item in dic)
{
Debug.Log