using LitJson; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class TableTest : MonoBehaviour { public List contents; [SerializeField] GameObject prefab; //预制体 [SerializeField] GameObject parent; //父物体(content) // Start is called before the first frame update void Start() { TextAsset jsonFile = Resources.Load("testfile"); string jsonString = jsonFile.text; JsonData jsonData = JsonMapper.ToObject(jsonString); Root data = JsonMapper.ToObject(jsonString); List subjectsInfoItems = data.subjectsInfo; List seatInfosItems = new List(); foreach (SubjectsInfoItem subjectsInfoItem in subjectsInfoItems) { if (subjectsInfoItem.seatInfos != null && subjectsInfoItem.seatInfos.Count() > 0) { seatInfosItems.AddRange(subjectsInfoItem.seatInfos); } Debug.Log(seatInfosItems.Count); } for (int i = 0; i < seatInfosItems.Count; i++) { GameObject item = Instantiate(prefab, parent.transform); contents.Add(item); contents[i].transform.GetChild(0).GetComponent().text = seatInfosItems[i].seatId; contents[i].transform.GetChild(1).GetComponent().text = seatInfosItems[i].seatName; contents[i].transform.GetChild(2).GetComponent().text = seatInfosItems[i].subjectId; } } }