-2

I'm developing a mobile game project that utilizes Firebase. I've managed to get login/registration, and sending items to the database to work just fine. However, after countless hours of banging my head against the wall, I just can't get the inventory to work. I can fetch the correct JSON data from the database, and it comes out in the following format:

{"5449000085757":{"itemLevel":82,"itemName":"Sword of Maximum Epicness","itemType":""},"6419800152996":{"itemLevel":45,"itemName":"Your Average Sword","itemType":""}}

These are all just fields for testing purposes of course. What I'm trying to do is create a list of objects, where the list is the "inventory" and the objects are the items. I've tried using Json.NET, Unity's built-in JSON utilities, all kinds of technologies, but just can't find a way. I feel kind of stupid, because obviously it can't be this difficult. No matter what example I've tried, it doesn't work.

If possible, could someone provide a quick briefing on how to create that list of objects from JSON, a simple way? I just can't figure this out on my own and it's getting really frustrating.

  • 2
    *No matter what example I've tried, it doesn't work.* So where is the code which doesnt work? It is *much* easier to help when you provide a starting point - as is, you've put all the burden on us. – Ňɏssa Pøngjǣrdenlarp Oct 20 '19 at 20:35
  • 1
    It looks to me like you should be deserializing into Dictionary where item has the fields itemLevel, itemName and itemType. – Tim Oct 20 '19 at 22:41
  • You should rethink the json shape as it has id as type name. It could work but I would not recommend it. Following may be more appropriate { "levels": [{"id": "5449000085757","itemLevel": 82,"itemName": "Sword of Maximum Epicness","itemType": ""},{"id": "6419800152996","itemLevel": 45,"itemName":"Your Average Sword","itemType": ""} ] } – Everts Oct 21 '19 at 12:45

3 Answers3

2

If you want a strongly typed class that you hydrate with your JSON you can do the following:

  1. Copy the JSON payload into your clipboard.
  2. In visual studio add a new class to your project. From the Edit menu select Paste Special / Paste JSON As Classes. This will create the class or classes that represent your JSON class.
  3. Open tools / Manage NuGet Packages for Solution. Add NewtonSoft.JSON library to your project.
  4. To hydrate your object from the JSON use something like this:

    MyObject myObject = JsonConvert.DeserializeObject(jsonPayload);

Greg the Incredulous
  • 1,676
  • 4
  • 29
  • 42
1

I would first download the Newtonsoft.Json NuGet package. Ensure you import it into your class with using Newtonsoft.Json.

Then I would create an Item class:

public class Item
{
    [JsonProperty("itemLevel")]
    public long ItemLevel { get; set; }

    [JsonProperty("itemName")]
    public string ItemName { get; set; }

    [JsonProperty("itemType")]
    public string ItemType { get; set; }
}

Then you can simply deserialize your JSON into a Dictionary<string, Item> using JsonConvert.DeserializeObject:

var deserializedJson = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);

Test

var json = "{\"5449000085757\":{\"itemLevel\":82,\"itemName\":\"Sword of Maximum Epicness\",\"itemType\":\"\"},\"6419800152996\":{\"itemLevel\":45,\"itemName\":\"Your Average Sword\",\"itemType\":\"\"}}";

var deserializedJson = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);

foreach (var entry in deserializedJson)
{
    Console.WriteLine($"Key={entry.Key}, itemLevel={entry.Value.ItemLevel}, itemName={entry.Value.ItemName}, itemType={entry.Value.ItemType}");
}

Output

Key=5449000085757, itemLevel=82, itemName=Sword of Maximum Epicness, itemType=
Key=6419800152996, itemLevel=45, itemName=Your Average Sword, itemType=
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

The best move would be deserializing it as Dictionary<string, object> and Iterate through the values and casting them to your desired "Item" class.