【问题标题】:JSON file not being read properly未正确读取 JSON 文件
【发布时间】:2021-10-08 19:01:47
【问题描述】:

我正在尝试读取 C# 中的 JSON 文件,我对它还很陌生。以下是相关 Json 的布局:

{
  "stickerBundles": [
    {
      "id": "ludoBundle",
      "stickers": [
        {
          "id": "compter",
          "title": "Compter",
          "code": "LUDO WS1001",
          "loadingScreenColor": "#",
          "assetBundles": [
            {
              "name": "animations",
              "assets": [ "Assets/AnimationCompter/Prefabs/Scene_Compter.prefab" ],
              "buildPath": [ "ar_animations-android-etc2", "ar_animations-ios-new" ]

            },
            {
              "name": "androidStory",
              "assets": [
                "Assets/Textures/Sprites/Histoires/Compter/Default/story-compter-alpha.spriteatlas",
                "Assets/Textures/Sprites/Histoires/Compter/Default/story-compter-noalpha.spriteatlas"
              ],
              "buildPath": [ "histoires/compter-android-etc2" ]
            },
            {
              "name": "iosNewStory",
              "assets": [
                "Assets/Textures/Sprites/Histoires/Compter/Default/story-compter-alpha.spriteatlas",
                "Assets/Textures/Sprites/Histoires/Compter/Default/story-compter-noalpha.spriteatlas"
              ],
              "buildPath": [ "histoires/compter-ios-new" ]
            },
            {
              "name": "iosOldStory",
              "assets": [
                "Assets/Textures/Sprites/Histoires/Compter/iOS/old/story-compter-alpha.spriteatlas",
                "Assets/Textures/Sprites/Histoires/Compter/iOS/old/story-compter-noalpha.spriteatlas"
              ],
              "buildPath": [ "histoires/compter-ios-old" ]
            }
          ],
          "downloads": [
            "ar_animations",
            "histoires/compter",
            "games/seekandfind"
          ]
        }, (...)

我正在尝试这样阅读:

public static StickerBundlesList GetStickersConfig()
    {
        string jsonString = File.ReadAllText(STICKER_CONFIG_FILENAME);
        return JsonUtility.FromJson<StickerBundlesList>(jsonString);
    }

我得到的对象是这样的:

{
    "stickerBundles": [
        {
            "id": "ludoBundle"
        }, (...)

意味着我不能使用除stickerBundles.id 之外的任何东西。

我为 StickerBundlesList(它只包含一个 StickerBundle 数组)和 StickerBundle、Sticker 和 Bundle 的结构创建了一个类。 StickerBundlesList 和 StickerBundle 是可序列化的。它们的外观如下:

StickerBundlesList.cs:

public partial class StickerResources
{
    [System.Serializable]
    public class StickerBundlesList
    {
        public StickerBundle[] stickerBundles;
    }
}

StickerBundle.cs:

public partial class StickerResources
{
    [System.Serializable]
    public struct StickerBundle
    {
        public string id;
        public Sticker[] stickers;
    }
}

StickerResources.cs:

public partial class StickerResources : SceneSingleton<StickerResources>
{
    public TextAsset textJson;
    public const string STICKER_CONFIG_FILENAME = "Assets/Resources/StickerResources/StickerResources.json";
    public struct Sticker
    {
        public string id;
        public string title;
        public string code;
        public string loadingScreenColor;
        public Bundle[] assetBundles;
    }
    public struct Bundle
    {
        public string name;
        public string[] assets;
        public string[] buildPath;
    }

    public StickerBundlesList stickerBundleList = new StickerBundlesList();

    private void Awake()
    {
        stickerBundleList = JsonUtility.FromJson<StickerBundlesList>(textJson.text);
    }

    public static StickerBundlesList GetStickersConfig()
    {
        string jsonString = File.ReadAllText(STICKER_CONFIG_FILENAME);
        return JsonUtility.FromJson<StickerBundlesList>(jsonString);
    }
}

我很想以 C# 对象的形式访问我的所有 JSON。我在这里做错了什么?

【问题讨论】:

  • +1 到问题的必要/几乎需要的输入来回答问题:-) 在查看提供的详细信息后,Sticker 似乎不存在 {System.Serializable]和Bundlestructs。我认为添加此序列化后应该可以工作。与问题无关,虽然我总是尽量避免在项目中使用单例,但它们导致的耦合比项目中的其他任何东西都多。
  • @nIcEcOw 哇,谢谢,成功了!

标签: c# json unity3d


【解决方案1】:

我认为问题的一部分可能是您的类/结构嵌套?可能暂时取消嵌套,直到序列化正常工作,然后如果你想稍后重新组织,再试一次。

我认为您并未包含所有源代码,因此我不得不进行一些推断。例如,我能够在 LINQPad 中使用它:

void Main()
{
    var jsonString = @"
{
  ""stickerBundles"": [

    {
        ""id"": ""ludoBundle"",
      ""stickers"": [

        {
            ""id"": ""compter"",
          ""title"": ""Compter"",
          ""code"": ""LUDO WS1001"",
          ""loadingScreenColor"": ""#"",
          ""assetBundles"": [

            {
                ""name"": ""animations"",
              ""assets"": [ ""Assets/AnimationCompter/Prefabs/Scene_Compter.prefab"" ],
              ""buildPath"": [ ""ar_animations-android-etc2"", ""ar_animations-ios-new"" ]

            }]
        }]
    }]
}
";
    //string jsonString = File.ReadAllText(STICKER_CONFIG_FILENAME);
    var result = JsonConvert.DeserializeObject<StickerBundlesList>(jsonString);
    
    result.Dump();
}

[System.Serializable]
public class StickerBundlesList
{
    public StickerBundle[] stickerBundles;
}

[System.Serializable]
public struct StickerBundle
{
    public string id;
    public Sticker[] stickers;
}

[System.Serializable]
public struct Sticker
{
    public string id;
    public string title;
    // etc
}

输出对象层次结构(StickerBundleList > StickerBundle > Sticker):

我使用 LINQPad 对此进行了测试,但您应该能够将其放入新的 C# 控制台应用程序或其他东西中进行测试。另外,我使用了 JSON.Net JSON 库。我猜我们的库中的差异不是是什么使这个工作,但如果你认为它可能是,检查你的库的设置。我正在使用 JSON.Net 的默认设置。

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多