先把JSON.dll文件拖到工程目录
创建一个txt文本然后拖到编辑器VS中进行编辑
JSON.TXT脚本编辑如下
{
"BOBO":
[
{
"id":"1001",
"name":"敏敏",
"sex":"女",
"age":"o1",
"hobby":"学习"
},
{
"id":"1002",
"name":"啵啵",
"sex":"男",
"age":"o2",
"hobby":"珍惜"
},
{
"id":"1003",
"name":"菲菲",
"sex":"女",
"age":"o3",
"hobby":"玩耍"
},
{
"id":"1004",
"name":"雨神",
"sex":"男",
"age":"o4",
"hobby":"计算机"
},
{
"id":"1005",
"name":"雨神",
"sex":"男",
"age":"o5",
"hobby":"计算机"
}
]
}
在场景中创建c#脚本并附给UI Root
编辑脚本如下
using UnityEngine;
using System.Collections;
using LitJson;
public class Jsonxi : MonoBehaviour {
public TextAsset Supperman;
// Use this for initialization
void Awake()
{
ReaderJson();
}
public void ReaderJson()
{
//找到对应的json数据
JsonData Bojs = JsonMapper.ToObject(Supperman.text);
//定义json数据赋值到JSON.txt中的名称
JsonData BoArray = Bojs["BOBO"];
//遍历数据
for (int i = 0; i < BoArray.Count; i++)
{
GameObject go = Instantiate(Resources.Load("bg",typeof(GameObject))) as GameObject;
go.transform.parent = GameObject.Find("Grid").transform;
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
go.transform.FindChild("photo").GetComponent<UISprite>().spriteName = BoArray[i]["age"].ToString();
go.transform.FindChild("id").GetComponent<UILabel>().text = BoArray[i]["id"].ToString();
go.transform.FindChild("name").GetComponent<UILabel>().text = BoArray[i]["name"].ToString();
go.transform.FindChild("sex").GetComponent<UILabel>().text = BoArray[i]["sex"].ToString();
go.transform.FindChild("hobby").GetComponent<UILabel>().text = BoArray[i]["hobby"].ToString();
}
}
}