简体   繁体   English

Newtonsoft.Json 反序列化只设置默认值,数据不正确

[英]Newtonsoft.Json Deserialization only sets default values, incorrect data

I am loading JSON data in my Unity project via Newtonsoft JsonConvert.Deserialize.我正在通过 Newtonsoft JsonConvert.Deserialize 在我的 Unity 项目中加载 JSON 数据。 This works in Editor and the json is valid.这在编辑器中有效,并且 json 有效。 However, as soon as I make a Windows build, the serialized data is empty / filled with default values instead.但是,一旦我进行 Windows 构建,序列化数据为空/填充默认值。

This is also shown when I serialize the deserialized data and output it:当我序列化反序列化数据和 output 时,也会显示这一点:

  • Json string read: 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": []
    }
  • reserialized:重新序列化:

    {
        "galleries": [{
            "id": null,
            "title": "",
            "slides": null,
            "path": "",
            "showAllInFolder": true,
            "foldersRecursive": true,
            "enableSlideshow": false,
            "slideshowSlideDuration": 5.0
        }],
        "cardMenus": []
    }

Here is the relevant C# class:这是相关的 C# class:


    [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() { }
    }

Edit for clarity: I am using UIContentData object to deserialize.为清楚起见进行编辑:我正在使用 UIContentData object 进行反序列化。

JsonConvert.DeserializeObject<UIContentData>(json);

Any help is much appreciated!任何帮助深表感谢!

You are using the wrong class,should create one more.您使用了错误的 class,应该再创建一个。 This code was tested and working properly此代码已经过测试并且可以正常工作

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; }
    }

and remove slides property from UIContentGallerySlide并从 UIContentGallerySlide 中删除 slides 属性

[JsonProperty("slides")]
    public List<UIContentGallerySlide> slides { get; set; }

Found the issue.发现问题。 Unity was stripping the class definitions I believe. Unity 正在剥离我相信的 class 定义。 Adding a link.xml with preserve="all" for the assembly containing them solved the problem!为包含它们的程序集添加一个带有preserve="all"的link.xml解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM