Json的读写数据详解步骤


先把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();
        }
       
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值