Unity加载json文本

Unity使用LitJson加载json文本

  1. 首先导入LitJson到Plugins文件夹下
  2. 创建json文本如下
    json文本
  3. 进行加载文本
json对象对应类型
[System.Serializable]
public class Item{
   public string mName;
   public int mWidth;
   public int mHeight;
   public int mNeedLv;
   public int mNeedGreen;
   public int mAddArea;
   public int mNeedGlod;
   public string mNeedTimer;
   public int mAddGold;
   public int mAddExp;
   
}
public class LoadJsonManager : MonoBehaviour
{
  private readonly string mPath="crops";//路径
  private List<Item> mItemList;
  private void Start() {
     mItemList=new List<Item>();
     TextAsset textAsset= Resources.Load<TextAsset>(mPath);
     JsonData data=JsonMapper.ToObject(textAsset.text);
     foreach(JsonData temp in data)
     {
     //如果temp是一个json数组还需要在进行一次转化再转换为JsonData
        string name=GetJsonValue(temp,"name");//这里的name是Json文本中的name 需要对应
        int width=int.Parse(GetJsonValue(temp,"width"));
        int height=int.Parse(GetJsonValue(temp,"height"));
        int needLv=int.Parse(GetJsonValue(temp,"needLv"));
        int needGreen=int.Parse(GetJsonValue(temp,"needGreen"));
        int addArea=int.Parse(GetJsonValue(temp,"AddArea"));
        int needGold=int.Parse(GetJsonValue(temp,"needGold"));
        string needTime=GetJsonValue(temp,"needTime");
        int addGold=int.Parse(GetJsonValue(temp,"AddGold"));
        int addExp=int.Parse(GetJsonValue(temp,"AddExp"));
        mItemList.Add(new Item(){mName=name,mWidth=width,mHeight=height,mNeedLv=needLv,mNeedGreen=needGreen,mAddArea=addArea,
        mNeedGlod=needGold,mNeedTimer=needTime,mAddGold=addGold,mAddExp=addArea});
     }
  }
  public string GetJsonValue(JsonData temp,string str)
  {
      return temp[str].ToString();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值