1

I have a JSON string like this:

{
  "ProfileChange": 5,
  "profileId": "anId",
  "profileChanges": [
    {
      "changeType": "fullProfileUpdate",
      "profile": {
        "_id": "g3fwerfgscsdadad",
        "rvn": 7,
        "wipeNumber": 5,
        "accountId": "gw4gefsdfswfwfwdqe",
        "profileId": "anId",
        "version": "latestUpdate",
        "items": {
          "abc-123": {
            "templateId": "default",
            "attributes": {
              "max": 0,
              "bonus": 1,
              "seen": 1,
              "xp": 0,
              "variants": [],
              "favorite": false
            },
            "quantity": 1
          },
          "zxc-456": {
            "templateId": "default",
            "attributes": {
              "max": 0,
              "bonus": 1,
              "seen": 1,
              "xp": 0,
              "variants": [],
              "favorite": false
            },
            "quantity": 1
          }
        }
      }
    }
  ]
}

I want to deserialize it to a C# object, and here is my code:

class Model
{
    [JsonProperty("ProfileChange")] public long ProfileChange { get; set; }

    [JsonProperty("profileId")] public string ProfileId { get; set; }

    [JsonProperty("profileChanges")] public List<ProfileChange> ProfileChanges { get; set; }
}

public class ProfileChange
{
    [JsonProperty("changeType")] public string ChangeType { get; set; }

    [JsonProperty("profile")] public Profile Profile { get; set; }
}

public class Profile
{
    [JsonProperty("_id")] public string Id { get; set; }

    [JsonProperty("rvn")] public long Rvn { get; set; }

    [JsonProperty("wipeNumber")] public long WipeNumber { get; set; }

    [JsonProperty("accountId")] public string AccountId { get; set; }

    [JsonProperty("profileId")] public string ProfileId { get; set; }

    [JsonProperty("version")] public string Version { get; set; }

    [JsonProperty("items")] public Items Items { get; set; }
}

public class Items
{
    public List<IDictionary<string, ItemDetails>> Item { get; set; }
}

public class ItemDetails
{
    [JsonProperty("templateId")] public string TemplateId { get; set; }
}

And then I implement it in my main implementation like this:

var obj = JsonConvert.DeserializeObject<Model>(json);

And it does deserialize most of the data, however, the part I am struggling to deserialize is the Items one. Since they are generated dynamically, I want to create a dictionary of the items, with string for the entry and ItemDetails for the details of it.

I have used Dictionary and also made it a list, since there are multiple items, but it still is null. I also tried non-list, didn't return anything. Only null. All other details are fine, except the dictionary and dynamically generated JSON data.

I need help to fix the issue with deserializer returning null for the Items Dictionary.

user9248102
  • 299
  • 2
  • 9
  • 1
    In `Profile` just do `[JsonProperty("items")] public Dictionary Items { get; set; }` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) or [Using JSON.NET to read a dynamic property name](https://stackoverflow.com/a/41390834/3744182). You don't need the `Items` type at all. – dbc Aug 09 '18 at 21:00
  • Just tested and it worked. Can't believe I didn't think about it. Thanks a lot for the help. Appreciate it very much! – user9248102 Aug 09 '18 at 21:02
  • Glad those answers worked! I went ahead and marked it as a dup. – dbc Aug 09 '18 at 21:04

0 Answers0