Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug JsonUtility in Unity not deserializing certain fields from JSON

Discussion in 'Unity Remote Config' started by mChapuis, Oct 5, 2023.

  1. mChapuis


    Jun 23, 2014
    I'm trying to deserialize a JSON string into a Unity struct using JsonUtility.FromJson<T>(), but certain fields like description, name, thumbnailAddress, and imageAddress always come out as null. Other fields are deserialized correctly.

    This is the function I use to fetch the configs from unity gaming services:

    Code (CSharp):
    1. void GetConfigValues()
    2. {
    3.     string shopCategoriesConfigJson = RemoteConfigService.Instance.appConfig.GetJson("VIRTUALSHOP_CONFIG_BASIC_02");
    4.     Debug.Log("raw json:"+shopCategoriesConfigJson.ToString());
    5.     virtualShopConfig = JsonUtility.FromJson<VirtualShopConfig>(shopCategoriesConfigJson);
    6.     Debug.Log(virtualShopConfig.ToString());
    7. }

    This is one line of the raw json:

    Code (CSharp):
    1. category:"TabType.TabTypes.candles", enabled:True, items: id:"VIRTUALSHOP_5_ANCIENTARCANE_FOR_AMIRITE" name:"" description:""thumbnailAddress:""imageAddress:""SizeType:_256x256, Style:natural, Currency:currencyTest1, ObjectType:cardSkin, Price:0, Level:0, TabType:favorites, TabSubGroup:staticTabs, TimeLimited:False

    Here's the struct I'm trying to deserialize into:
    Code (CSharp):
    1. [Serializable]
    2. public struct ItemConfig
    3. {
    4.     public string id;
    5.     public string name;
    6.     public string description;
    7.     public string thumbnailAddress; // addressableManager has the sprite
    8.     public string imageAddress;     // addressableManager has the sprite
    9.     // ... [other fields] ...
    10. }

    Code (CSharp):
    2. And here's a sample of the JSON I'm trying to deserialize:
    3. {
    4.     "currency": 0,
    5.     "description": "Beautiful Amirite test",
    6.     "imageAddress": "Assets/Models/Items/Sprites/amirite_256x256.png",
    7.     "level": 0,
    8.     "name": "Amirite",
    9.     "objectType": 1,
    10.     "price": 5,
    11.     "sizeType": 0,
    12.     "style": 0,
    13.     "tabSubGroup": 2,
    14.     "tabType": 3,
    15.     "thumbnailAddress": "Assets/Models/Items/Sprites/amirite_256x256.png",
    16.     "timeLimited": "yes"
    17. }

    Some observations:

    1- timeLimited field is a boolean. I wrote "no" in the json but the result is False. There is some sort of conversion?

    2- the id:"VIRTUALSHOP_5_ANCIENTARCANE_FOR_AMIRITE" is the ID from the virtual purchase that links a currency and the item. So its not the id of the item here however, it fetch correctly some values that are included in the item json only such as tabType, price, etc.

    3- The only issues are with the 4 strings. The rest is fine.

    p.s: also posted this on stackoverflow
  2. CodeSmile


    Apr 10, 2014
    That raw json has syntax errors. For example "items: id:" is not correct, opening brackets are missing there.

    timeLimited in the json is a string "yes" and not a bool, as denoted by the quotation marks. Actual bools are printed as either true or false without quotation.
    GabKBelmonte likes this.
  3. mChapuis


    Jun 23, 2014
    Yes you are right. I need to fix this.

    I found a temporary fix by moving the location of the field name, imageAddress and thumbnailAddress and now it works. Instead of having them in the Inventory Item Json, I added them to the remoteConfig Json. Its not what I want but its working for now.