Unity学习笔记--解决使用JsonUtility读取Json文件时报空指针的问题

可能是没有序列化。
加上类的上面加上[Serializable]就可以了。

如果是这样的格式:

{
    
    
    {
    
    
    "StudentDataList":
    [
        {
    
    
            "Id":1,
            "Name":"张三",
            "StudentObjectScore":
            [
                {
    
    
                    "ObjectName":"Chinese",
                    "Score":80
                },
                {
    
    
                    "ObjectName":"Math",
                    "Score":90
                },
                {
    
    
                    "ObjectName":"English",
                    "Score":60
                }
            ]
        }
    ]
}

就需要这样写:

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

[Serializable]
public class StudentDataWithJsonItem
{
    
    
    public int Id;
    public string Name;
    public List<StudentObjectScore> StudentObjectScore;
}

[Serializable]
public class StudentObjectScore
{
    
    
    public string ObjectName;
    public int Score;
}

public class StudentData
{
    
    
    public List<StudentDataWithJsonItem> StudentDataList;
}


我理解的是Json里面只要在 [] 里面的数据,那么就需要序列化

猜你喜欢

转载自blog.csdn.net/qq_52855744/article/details/119191615
今日推荐