【发布时间】:2023-04-02 23:13:01
【问题描述】:
我在解析这个 json 时遇到问题:
{
"product_info":
{
"title": "Product Name"
}
}
这是我的代码:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using UnityEngine.UI;
public class ReadJson : MonoBehaviour
{
public Text myText;
[System.Serializable]
public class ProductInfo
{
public string title { get; set; }
}
[System.Serializable]
public class RootObject
{
public ProductInfo product_info { get; set; }
}
void Start () {
TextAsset asset = Resources.Load (Path.Combine ("Json", "toulouse")) as TextAsset;
RootObject m = JsonUtility.FromJson<RootObject> (asset.text);
Debug.Log (m.product_info.title);
}
}
我收到此错误消息:“对象引用未设置为对象的实例”。我已经尝试过,成功解析了一个非嵌套的 json,但我不明白为什么,但即使在创建了适当的类之后也不起作用。
【问题讨论】:
-
如果你将RootObject定义为一个动态对象,这能帮助你找出必要的结构吗?