孩子实在改不动了寻求大佬帮助

Thechocolatemelt 2023-04-23 12:15:08

这段代码是我从网上借用别人进行测试的,但不知道为什么一运行就会报出无法转化类型的错误,litjson的版本也都改过了

 

using UnityEngine;
using LitJson;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class TiggerTest : MonoBehaviour
{
    public void Start()
    {
        //json读取,读取Assets/Json/test.json文件
        string str = File.ReadAllText(Application.dataPath + "/Json/test.json");
        JsonData nameArray = JsonMapper.ToObject(str);
        foreach (JsonData jd in nameArray)
        {
            Debug.Log(jd["name"]);
        }

        //json写入,写入Assets/Json/item.json文件
        Item item1 = new Item(10, "sword");
        Item item2 = new Item(1, "cap");
        List<Item> items = new List<Item> { item1, item2 };
        File.WriteAllText(Application.dataPath + "/Json/item.json", JsonMapper.ToJson(items));
    }
}
[System.Serializable]   //Item类需要可序列化
public class Item
{
    public int id;
    public string name;
    public Item(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

 

...全文
257 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,589

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧