我正在通过Newtonsoft JsonConvert在Unity项目中加载JSON数据。反序列化。这在Editor中有效,json是有效的。然而,一旦我构建了Windows,序列化的数据就会为空/填充默认值。
当我序列化反序列化的数据并输出它时,这也会显示出来:
- Json字符串读取:
{
"galleries": [{
"id": "Acker",
"title": "Auf dem Acker",
"slides": [],
"path": "/ChangeableContentData/Auf dem Acker",
"showAllInFolder": true,
"foldersRecursive": false,
"enableSlideshow": false,
"slideshowSlideDuration": 5.0
}],
"cardMenus": [],
"urls": []
}
- 已预订:
{
"galleries": [{
"id": null,
"title": "",
"slides": null,
"path": "",
"showAllInFolder": true,
"foldersRecursive": true,
"enableSlideshow": false,
"slideshowSlideDuration": 5.0
}],
"cardMenus": []
}
以下是相关的C#类:
[System.Serializable]
[Preserve]
public partial class UIContentData
{
[JsonProperty("galleries")]
public List<UIContentGalleryData> galleries { get; set; }
[JsonProperty("cardMenus")]
public List<UIContentCardMenuData> cardMenus { get; set; }
[JsonProperty("urls")]
public List<UIContentUrlData> urls { get; set; }
public UIContentData() { }
}
[System.Serializable]
[Preserve]
public partial class UIContentGalleryData
{
public string id { get; set; }
[JsonProperty("title")]
public string title { get; set; } = ""; // optional
[JsonProperty("slides")]
public List<UIContentGallerySlide> slides { get; set; }
[JsonProperty("path")]
public string baseFolderPath { get; set; } = "";
[JsonProperty("showAllInFolder")]
public bool showAllInFolder { get; set; } = true; // optional
[JsonProperty("foldersRecursive")]
public bool foldersRecursive { get; set; } = true; // optional
[JsonProperty("enableSlideshow")]
public bool enableSlideshow { get; set; } = false; // optional
[JsonProperty("slideshowSlideDuration")]
public float slideshowSlideDuration { get; set; } = 5f; // optional
public UIContentGalleryData() { }
}
为了清楚起见,请编辑:我正在使用UIContentData对象进行反序列化。
JsonConvert.DeserializeObject<UIContentData>(json);
非常感谢您的帮助!
您使用了错误的类,应该再创建一个。此代码经过测试,工作正常
var data=JsonConvert.DeserializeObject<Root>(json);
public class Root
{
public List<UIContentGalleryData> galleries { get; set; }
public List<object> cardMenus { get; set; }
public List<object> urls { get; set; }
}
并从UIContentGallerySlide 中删除幻灯片属性
[JsonProperty("slides")]
public List<UIContentGallerySlide> slides { get; set; }
发现问题。Unity剥离了我认为的类定义。添加具有preserve="的link.xml;所有";因为包含它们的程序集解决了问题!