我遇到了一个问题,无法从一个JSON文件中的C#(Unity)对象中访问字符串。

huangapple go评论49阅读模式
英文:

I am facing a problem with accessing the strings in an object from a json file in c# (Unity)

问题

I understand that you're encountering an issue with accessing the content from the JSON file in your Unity script. Let's focus on the translation of the code:

[System.Serializable]
public class DialogueData
{
    public Dialogue dialogue;
}

[System.Serializable]
public class Dialogue
{
    public List<DialogueContent> fireDialogue;
}

[System.Serializable]
public class DialogueContent
{
    public string content { get; set; }
    public string letterDelay { get; set; }
    public string fontColour { get; set; }
} 
public class CharacterInteraction : MonoBehaviour
{
    public DialogueData dialogueData;
    public TextAsset jsonFile;

    void Start()
    {
        jsonFile = Resources.Load<TextAsset>("dialogue");
        dialogueData = JsonUtility.FromJson<DialogueData>(jsonFile.text);

        fireDialogueOrder = 0; // should access the first index of the list

        //the following Debug.Log() returns an empty string for some reason
        Debug.Log(dialogueData.dialogue.fireDialogue[fireDialogueOrder].content);
    }
}

If you have any further questions or need additional assistance, feel free to let me know!

英文:

So my problem is that when I try to access the json class' contents it returns an empty string value.
First of all, I'm trying to create a dialogue system. I'm trying to save the lines and information about the lines in strings in a json file. (The character is a bonfire, which is why it's called fireDialogue)
As you can see in the following json file, the object for one line of dialogue consists of three strings, one that actually contains the line, one that contains how fast that dialogue is going to be shown, and one that contains the font colour. (I wanted to be able to make specific segments of a line appear faster and have more possibilities, so that's why I added the |, so I can .Split it later).
I'm going to send the relevant code now, maybe anyone of you can spot what's causing the error:

json file:

{
    &quot;dialogue&quot;: {
        &quot;fireDialogue&quot;: [
            {
                &quot;content&quot;: &quot;Hello there&quot;,
                &quot;letterDelay&quot;: &quot;30&quot;,
                &quot;fontColour&quot;: &quot;&quot;
            },
            {
                &quot;content&quot;: &quot;I talk |slow |and |fast&quot;,
                &quot;letterDelay&quot;: &quot;30|50|30|10&quot;,
                &quot;fontColour&quot;: &quot;&quot;
            }
        ],
}

json class:

[System.Serializable]
public class DialogueData
{
    public Dialogue dialogue;
}

[System.Serializable]
public class Dialogue
{
    public List&lt;DialogueContent&gt; fireDialogue;
}

[System.Serializable]
public class DialogueContent
{
    public string content {get; set; }
    public string letterDelay {get; set; }
    public string fontColour {get; set; }
} 

The script, where I'm trying to access the json:

public class CharacterInteraction : MonoBehaviour
{
    public DialogueData dialogueData;
    public TextAsset jsonFile;
    void Start()
    {

        jsonFile = Resources.Load&lt;TextAsset&gt;(&quot;dialogue&quot;);

        dialogueData = JsonUtility.FromJson&lt;DialogueData&gt;(jsonFile.text);


        fireDialogueOrder = 0; // should access the first index of the list

        //the following Debug.Log() returns an empty string for some reason
        Debug.Log(dialogueData.dialogue.fireDialogue[fireDialogueOrder].content);
    }
}

I have checked several online sources and have put my json file into a website that checks for errors and it hasn't found any. I also checked if the json class is correct and if I correctly referred to it in the script in which I am accessing the values, but nothing stood out to me. Obviously, I also quadruple checked, if the file names are correct. Also neither Visual Studio Code nor Unity show any error messages. The result is just wrong.

I asked friends, who have way more experience with programming than me and they also didn't have any idea, except that it must have something to do with the serialization.

I tried removing the object in the json that contains three strings and directly put string values inside the fireDialogue object, so it would be a list of strings, instead of a list of objects, which worked fine. The problem seems to have something to do with me using objects.

I hope I have provided enough information for you to understand the problem.
I am thankful for any help!

答案1

得分: 0

由于您正在使用JsonUtility,您需要从属性中删除get/set。

[System.Serializable]
public class DialogueContent
{
    public string content;
    public string letterDelay;
    public string fontColour;
}

但我建议您查找Unity的NuGet包Newtonsoft.Json并安装它。在这种情况下,您需要为一些属性添加get set。

using Newtonsoft.Json;

dialogueData = JsonConvert.DeserializeObject<DialogueData>(jsonFile.text);

public class DialogueData
{
    public Dialogue dialogue { get; set; }
}

public class Dialogue
{
    public List<DialogueContent> fireDialogue { get; set; }
}
英文:

since you are using JsonUtility you have to remove get/set from your properties

[System.Serializable]
public class DialogueContent
{
    public string content ;
    public string letterDelay ;
    public string fontColour ;
} 

but instead I recommend you to find a nuget package Newtonsoft.Json for Unity and install it. In this case you will need to add get set to some of your properties;

using Newtonsoft.Json;

dialogueData = JsonConvert.DeserializeObject&lt;DialogueData&gt;(jsonFile.text);

public class DialogueData
{
    public Dialogue dialogue {get; set;}
}

public class Dialogue
{
    public List&lt;DialogueContent&gt; fireDialogue {get; set;}
}

huangapple
  • 本文由 发表于 2023年4月17日 00:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定