【发布时间】: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 哇,谢谢,成功了!