简体   繁体   English

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

[英]How to serialize and deserialize complex nested json Unity?

I have the following json我有以下 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": ["☕","🙂"]
        }

      ]
    }
  ]
}

I have no acquaintance with json until now, How can i deserialize this?到目前为止,我对 json 不熟悉,我该如何反序列化?

I know how to do the basic read and write code from persistent data path of unity.我知道如何从统一的持久数据路径进行基本的读写代码。 But how do i process this json?但是我该如何处理这个 json?

My main goal is as the player wins a level, a new key and value would be added to the "stickers" attribute, Also after some levels I want to add changes to the sticker packs attribute later.我的主要目标是当玩家赢得一个关卡时,一个新的键和值将被添加到“贴纸”属性中,而且在一些关卡之后,我想稍后添加对贴纸包属性的更改。

Plus how will i modify the value of image data version in a specific sticker pack item?另外,我将如何修改特定贴纸包项目中图像数据版本的值?

Thanks in advance提前致谢

you can use Newtonsoft.Json library to deserialize and serialize.您可以使用 Newtonsoft.Json 库进行反序列化和序列化。 Find below the respective C# class.在下面找到相应的 C# class。

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

Code to Deserialize:反序列化代码:

Root root = JsonConvert.DeserializeObject<Root>(json);

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

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