c# - 我无法从 JSON 获取数据并且没有错误
问题描述
我正在尝试从 json 文件中仅获取一个对象,但不能。在检查器中,对象是空的,当我试图做某事时,该对象因为它是空的而得到空异常。我还尝试将我试图检索的对象放入列表中,但列表仍然是空的。我的错误在哪里?我的 Json 文件如下所示:
{
"Dialogue": {
"npcName": "John",
"sentences": [
{
"text1": "Hello, come here",
"text2": "How do you feel",
"text3": " good bye!"
}
]
}
}
这是我的代码:
public TextAsset jsonScript;
public Dialogue dialogue;
void Start()
{
try
{
dialogue = JsonUtility.FromJson<Dialogue>(jsonScript.text);
}
catch (System.Exception error)
{
Debug.LogError(error);
}
}
对话课:
[System.Serializable]
public class Dialogue
{
public string npcName;
public string[] sentences;
}
解决方案
原始 json 中的句子是具有属性的复杂对象数组text1, text2, text3
你的 json 应该看起来像
{
"npcName": "John",
"sentences": [
"Hello, come here", "How do you feel", " good bye!"
]
}
适合Dialogue
类型。
推荐阅读
- c++ - 如何将“10:10”翻译成纪元格式
- gcc - avr-gcc 版本:不会从 5.4 更新到 9.2
- python-3.x - Python:请求中的 POST 数据问题
- office-js - Microsoft Outlook O365“加载项错误”自动重启
- c# - 更改 datetimepicker 背景颜色 C#
- freemarker - Freemarker (FTL) - 如何检查字符串是否可以使用 ?number 转换为数字
- vhdl - inout 端口在同一个应用程序中是否可以同时在 in 和 out 中工作
- java - 如何修复 java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)
- powershell - 我们可以使用 Powershell 删除 PowerBI 仪表板吗
- python - sqllite3 中的参数