Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JsonUtility将Json文件转换为列表。[unity][c#]_C#_Unity3d_Unityscript - Fatal编程技术网

使用JsonUtility将Json文件转换为列表。[unity][c#]

使用JsonUtility将Json文件转换为列表。[unity][c#],c#,unity3d,unityscript,C#,Unity3d,Unityscript,我正在尝试使用json.Utilities将GameInfo.json文件转换为列表数组。我想处理这个列表数据。 我的项目由5部分组成(日期、锦标赛名称、pos、回合、总分) 这是我的Json文件(GameInfo.Json) 游戏信息类包含5个数据,游戏信息列表类将用于创建列表结构。GameInfo.json文件已转换为GameInfo。 之后,游戏信息存储为字符串类型(jsonstring) 通过使用unity.utilities,我尝试将字符串数据存储到列表中,以便操作数据。 但是,我无法

我正在尝试使用json.Utilities将GameInfo.json文件转换为列表数组。我想处理这个列表数据。
我的项目由5部分组成(日期、锦标赛名称、pos、回合、总分) 这是我的Json文件(GameInfo.Json)

游戏信息类包含5个数据,游戏信息列表类将用于创建列表结构。GameInfo.json文件已转换为GameInfo。 之后,游戏信息存储为字符串类型(jsonstring) 通过使用unity.utilities,我尝试将字符串数据存储到列表中,以便操作数据。 但是,我无法到达foreach循环(我看不到Debug.Log消息)。 你能帮我解决我的问题吗。谢谢你的阅读

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class GameInfoList
{
    public GameInfo gameInfo;
    public string[,] Items = new string[2,5];
    public List<GameInfo> list = new List<GameInfo>();
}

[System.Serializable]
public class GameInfo
{
    public string date;
    public string tournamentName;
    public string pos;
    public string rounds;
    public string totalScore;
}

public class JsonDataConverter : MonoBehaviour
{
    public string path = "GameInfo.json";
    public string jsonstring;
    GameInfoList gameInfoList;
    public string JsonFileToString(string path)
    {
        Debug.Log("test");
        string loadJsonfile = path.Replace(".json", "");
        TextAsset loadedfile = Resources.Load<TextAsset>(loadJsonfile);
        jsonstring = loadedfile.text;
        Debug.Log("jsonstring:" + jsonstring);
        return jsonstring;
    }

    public void Awake()
    {
        Debug.Log("testawake");
        jsonstring = JsonFileToString(path);
        gameInfoList = JsonUtility.FromJson<GameInfoList>(jsonstring);
    }

    public void Start()
    {
        foreach (var object in gameInfoList.list)
        {
            Debug.Log("reach to Inside Loop");
            Debug.Log(object .date);
            Debug.Log(object .pos);
            Debug.Log(object .rounds);
            Debug.Log(object .totalScore);
        }
    } 
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[系统可序列化]
公共类游戏信息列表
{
公共游戏信息;
公共字符串[,]项=新字符串[2,5];
公共列表=新列表();
}
[系统可序列化]
公共类游戏信息
{
公共字符串日期;
公共字符串锦标赛名称;
公共字符串pos;
公共串轮;
公共字符串总分;
}
公共类JsonDataConverter:MonoBehavior
{
公共字符串path=“GameInfo.json”;
公共字符串jsonstring;
GameInfoList GameInfoList;
公共字符串JsonFileToString(字符串路径)
{
调试日志(“测试”);
string loadJsonfile=path.Replace(“.json”,”);
TextAsset-loadedfile=Resources.Load(loadJsonfile);
jsonstring=loadedfile.text;
Log(“jsonstring:+jsonstring”);
返回jsonstring;
}
公共图书馆
{
Debug.Log(“testawake”);
jsonstring=JsonFileToString(路径);
gameInfoList=JsonUtility.FromJson(jsonstring);
}
公开作废开始()
{
foreach(gameInfoList.list中的var对象)
{
Log(“到达内部循环”);
Debug.Log(object.date);
Debug.Log(object.pos);
Debug.Log(object.rounds);
Debug.Log(object.totalScore);
}
} 
}

“参数超出范围”表示“gameInfoList”等于null,我认为问题出在这一行

gameInfoList = JsonUtility.FromJson<GameInfoList>(jsonstring);
gameInfoList=JsonUtility.FromJson(jsonstring);
我认为json字符串只能转换为如下所示的对象GameInfoList:

[System.Serializable]
public class GameInfoList
{
    public List<GameInfo> Items;
}
[System.Serializable]
公共类游戏信息列表
{
公共清单项目;
}

我不太熟悉u3d,但我认为您可以在Awake()的末尾进行调试,以检查gameinfo.list[0].date等是否正确,并在foreach块之前进行调试,以检查Start()是否已执行。我调试了.Log(gameinfo.list[0].date),但我收到以下消息ArgumentOutOfRangeException:参数超出范围。参数名称:index System.Collections.Generic.List`1[GameInfo].get_Item(Int32 index)(at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)JsonDataConverter.Awake()(at Assets/JsonDataConverter.cs:43)您的json名称“Items”甚至与您的类名“GameInfo”不匹配。粘贴该json,您将获得正确的类。确保从中删除
{get;set;}
。有关如何读取json,请参见重复问题。
[System.Serializable]
public class GameInfoList
{
    public List<GameInfo> Items;
}