skip to Main Content

I’m trying to deserialize a JSON string into a Unity struct using JsonUtility.FromJson(), 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:

void GetConfigValues()
{
    string shopCategoriesConfigJson = RemoteConfigService.Instance.appConfig.GetJson("VIRTUALSHOP_CONFIG_BASIC_02");
    Debug.Log("raw json:"+shopCategoriesConfigJson.ToString());
    virtualShopConfig = JsonUtility.FromJson<VirtualShopConfig>(shopCategoriesConfigJson);
    Debug.Log(virtualShopConfig.ToString());
}

This is one line of the raw json:

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 (based on the example from unity documentation:https://docs.unity.com/ugs/solutions/manual/VirtualShops ):

[Serializable]
public struct VirtualShopConfig
{
    public List<CategoryConfig> categories;
    public List<PurchaseConfig> purchases;

    public override string ToString()
    {
        return $"categories: {string.Join(", ", categories.Select(category => category.ToString()).ToArray())}, " +
           $"purchases: {string.Join(", ", purchases.Select(purchase => purchase.ToString()).ToArray())}";
    }
}

[Serializable]
public struct CategoryConfig
{
    public string id;
    public bool enabledFlag;
    public List<ItemConfig> items;

    public override string ToString()
    {
        var returnString = new StringBuilder($"category:"{id}", enabled:{enabledFlag}");
        if (items?.Count > 0)
        {
            returnString.Append($", items: {string.Join(", ", items.Select(itemConfig => itemConfig.ToString()).ToArray())}");
        }

        return returnString.ToString();
    }
}



[Serializable]
public struct ItemConfig
{
    public string id;
    public string name;
    public string description;
    public string thumbnailAddress; // addressableManager has the sprite
    public string imageAddress;     // addressableManager has the sprite
    // ... [other fields] ...
}

And here’s a sample of the JSON I’m trying to deserialize:

{
    "currency": 0,
    "description": "Beautiful Amirite test",
    "imageAddress": "Assets/Models/Items/Sprites/amirite_256x256.png",
    "level": 0,
    "name": "Amirite",
    "objectType": 1,
    "price": 5,
    "sizeType": 0,
    "style": 0,
    "tabSubGroup": 2,
    "tabType": 3,
    "thumbnailAddress": "Assets/Models/Items/Sprites/amirite_256x256.png",
    "timeLimited": "yes"
}

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.

2

Answers


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

    A boolean in JSON has the format

    "name":true
    

    or accordingly

    "name":false
    

    without any " wrapping the value. It will always fallback to the default value – which would be false.

    the id:"VIRTUALSHOP_5_ANCIENTARCANE_FOR_AMIRITE"

    does nowhere appear in your given JSON so I don’t really get what the issue is here.

    The only issues are with the 4 strings.

    Tbh I can’t see an issue with them regarding your implementation.

    Are you sure you even get those from the API you are using?

    The log line you showed us (This is one line of the raw json:) according to the format seems to be NOT the raw json from

    Debug.Log("raw json: {shopCategoriesConfigJson});
    

    but from

    Debug.Log(virtualShopConfig.ToString());
    

    If the string are already missing in the first one, than I would assume there is rather an issue with that API itself rather than the JSON deserialization.

    Login or Signup to reply.
  2. Change the structures to classes. The JsonUtility doesn’t support all JSON features, such as nested objects or arrays directly within structs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search