示例json串
{
"dataList":[
{
"type": 1,
"name": "额头",
"icon": "forehead_icon@3x",
"smallDataList": [
{
"type": 0,
"name": "额头宽度",
"resList": [
"forehead_narrow",
"forehead_wide"
],
"icon": "forehead_width_icon@3x"
},
{
"type": 1,
"name": "额头前后",
"resList": [
"forehead_deep",
"forehead_shallow"
],
"icon": "forehead_depth_icon@3x"
}
]
},
{
"type": 1,
"name": "耳朵",
"icon": "ear_icon@3x",
"smallDataList": [
{
"type": 0,
"name": "耳朵高度",
"resList": [
"earlobe_short",
"earlobe_long"
],
"icon": "ear_height_icon@3x"
},
{
"type": 1,
"name": "耳朵宽度",
"resList": [
"ears_narrow",
"ears_wide"
],
"icon": "ear_width_icon@3x"
}
]
}
]
}
C#解析
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.IO;
using System;
public class AdjustSmallData
{
public int type;
public string name;
public string icon;
public List<string> targetPath;
}
public class AdjustData
{
public int type;
public string name;
public string icon;
public List<AdjustSmallData> smallDataList;
}
public class ReadJson : MonoBehaviour
{
private List<AdjustData> dataList = new List<AdjustData>();
private void Start()
{
Debug.Log("Start");
for (var i = 0; i < hideBodyTogList.Count; ++i)
{
var tempI = i;
hideBodyTogList[i].onValueChanged.AddListener((selected) =>
{
_body_example.ShowBodyPart(tempI, selected);
});
}
//初始化数据
Debug.Log("初始化脸部细节数据****************");
var textString = Resources.Load<TextAsset>("faceConfig");
var tempDic = MiniJSON.Json.Deserialize(textString.ToString()) as Dictionary<string, object>;
var list = tempDic["dataList"] as List<object>;
for (var i = 0; i < list.Count; ++i)
{
var temp = list[i] as Dictionary<string, object>;
var data = new AdjustData()
{
type = i,
name = temp["name"].ToString(),
icon = temp["icon"].ToString(),
smallDataList = new List<AdjustSmallData>()
{
}
};
var smallList = temp["smallDataList"] as List<object>;
for (var j = 0; j < smallList.Count; ++j)
{
var tempSmall = smallList[j] as Dictionary<string, object>;
var resList = tempSmall["resList"] as List<object>;
data.smallDataList.Add(new AdjustSmallData
{
type = j,
name = tempSmall["name"].ToString(),
icon = tempSmall["icon"].ToString(),
targetPath = new List<string> {
resList[0] as string,
resList[1] as string,
}
});
}
dataList.Add(data);
}
Debug.Log("初始化脸部细节数据****************完成");
}
}