如何序列化和反序列化复杂的嵌套json Unity?

问题描述 投票:0回答:1

我有以下json

{
  "android_play_store_link": "xyz",
  "ios_app_store_link": "",
  "sticker_packs": [
    {
      "identifier": "1",
      "name": "abc",
      "publisher": "Jane Doe",
      "tray_image_file": "xyz.png",
      "image_data_version":"1",
      "avoid_cache":false,
      "publisher_email":"",
      "publisher_website": "",
      "privacy_policy_website": "",
      "license_agreement_website": "",
      "stickers": [
        {
          "image_file": "abc.webp",
          "emojis": ["☕","🙂"]
        },
        {
          "image_file": "cdf.webp",
          "emojis": ["😩","😰"]
        },
        {
          "image_file": "efg.webp",
          "emojis": ["☕","🙂"]
        }

      ]
    }
  ]
}

直到现在我都不了解json,如何反序列化呢?

我知道如何从统一的持久数据路径进行基本的读写代码。但是我该如何处理这个json?

我的主要目标是,当玩家赢得一个关卡时,会将新的键和值添加到“ stickers”属性中。同样,在某些关卡之后,我想稍后再对贴纸包属性进行更改。

另外,我将如何修改特定贴纸包项目中图像数据版本的值?

提前感谢

c# android json unity3d json.net
1个回答
0
投票

您可以使用Newtonsoft.Json库进行反序列化和序列化。在相应的C#类下面找到。

public class Sticker
{
    public string image_file { get; set; }
    public IList<string> emojis { get; set; }
}

public class StickerPack
{
    public string identifier { get; set; }
    public string name { get; set; }
    public string publisher { get; set; }
    public string tray_image_file { get; set; }
    public string image_data_version { get; set; }
    public bool avoid_cache { get; set; }
    public string publisher_email { get; set; }
    public string publisher_website { get; set; }
    public string privacy_policy_website { get; set; }
    public string license_agreement_website { get; set; }
    public IList<Sticker> stickers { get; set; }
}

public class Root
{
    public string android_play_store_link { get; set; }
    public string ios_app_store_link { get; set; }
    public IList<StickerPack> sticker_packs { get; set; }
}

要序列化的代码:

Root root = JsonConvert.DeserializeObject<Root>(json);
© www.soinside.com 2019 - 2024. All rights reserved.