A9191
A9191

Reputation: 23

Deserializing JSON with numeric string keys in C#

{
  Items: [
    {
      "title": "Object1",
      "preview": {
        "2048": "preview_9212.jpg",
        "1024": "preview_6693.jpg",
      }
    },
    {
      "title": "Object2",
      "preview": {
        "2048": "preview_9888.jpg",
        "1024": "preview_6890.jpg",
      }
    },
    {
      "title": "Object3",
      "preview": {
        "2048": "preview_9822.jpg",
        "1024": "preview_6848.jpg",
      }
    }
  ]
}

I usually deserialise like this:

[Serializable]
public class JsonParser
{
    public string title;
    public List<Preview> preview;
}

[Serializable]
class Preview
{
    public string 2048;
} 

But since 2048 is an Integer is not possible to use this way. I tried to deserialize the JSON to get preview like these:

public class Preview
{
  [JsonProperty("2048")]
  public string imageNumber { get; set; }
}

var user = JsonConvert.DeserializeObject<Preview>(jsonValue);

or

var json = JObject.Parse(jsonValue);
var preview = json["preview"].ToObject<Dictionary<string, string>>();
foreach (var entry in preview)
{
    Debug.Log(entry.Key);
    Debug.Log(entry.Value);
}

I got: NullReferenceException: Object reference not set to an instance of an object.

I also tried Deserializing JSON that has an int as a key in C# but again NullReferenceException;

Thanks for any help!

Upvotes: 0

Views: 883

Answers (1)

Serge
Serge

Reputation: 43860

Since you have numeric string properties, you have 2 main choices:

  1. Use something like [JsonProperty("2048")] and select valid name for the property

  2. Or use a dictionary. This looks much more flexible for me, so you can try this code

Data data= JsonConvert.DeserializeObject<Data>(json);

string preview2048 = data.Items[0].Preview["2048"];  //preview_9212.jpg

or more complicated search using Linq

string  obj3Preview2048 = data.Items.Where(i=> i.Title == "Object3")
.Select(i =>i.Preview["2048"]).FirstOrDefault(); //preview_9822.jpg

classes

public partial class Data
{
    [JsonProperty("Items")]
    public List<Item> Items { get; set; }
}

public partial class Item
{
    [JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
    public string Title { get; set; }

    [JsonProperty("token")]
    public string Token { get; set; }

    [JsonProperty("preview")]
    public Dictionary<string, string> Preview { get; set; }
}

and you have some typos in json you posted, and I fix "type" to "title" in one of json objects. This is a fixed version

{
    "Items": [{
            "title": "Object1",
            "token": "6561b1bbe5f1958848jhgd43d2",
            "preview": {
                "2048": "preview_9212.jpg",
                "1024": "preview_6693.jpg"
            }
        },
        {
            "title": "Object2",
            "token": "4a42eb54648DSFhUI664654d25",
            "preview": {
                "2048": "preview_9888.jpg",
                "1024": "preview_6890.jpg"
            }
        },
        {
            "type": "Object3",
            "token": "3fba64831dghkjgfkl5dfaegoj9",
            "preview": {
                "2048": "preview_9822.jpg",
                "1024": "preview_6848.jpg"
            }
        }
    ]
}

Upvotes: 1

Related Questions