1

Im using C# and I need a dictionary with a string Key and a int value. Im using EasyJson to deserialize/serialize. This dictionary is inside a class:

public class LevelManager : MonoBehaviour
{    
    public float version  { get; set; };
    public int health  { get; set; };
    public Dictionary<string,int> powers  { get; set; };
}

But I couldn't make a JSON that meets the dictionary format. Every time I use an option I get a cast error I tried this structures (just copying the powers section):

"powers": [
  {"Hadoken": 15},
  {"Electricity": 20},
  {"Rocket": 25},
  {"Kick": 20}
]

Another try:

"powers": {
  "Hadoken": 15,
  "Electricity": 20,
  "Rocket": 25,
  "Kick": 20
}

And I had many more. What I'm missing? Which is the correct way to format a dictionary in JSON?

David TG
  • 85
  • 3
  • 10
  • 33
  • As far as I know, a regular list doesn't get string index. – David TG May 10 '19 at 12:31
  • 1
    What you could do is: create a dictionary, add some entries and then call the `serialize` method of the library. This should give you exactly the format you are looking for – Lennart Stoop May 10 '19 at 12:33
  • What was wrong with the 2nd one, because that looks right? – Reinstate Monica Cellio May 10 '19 at 12:42
  • @Archer I did and I get something really specific to C#, I was looking a standard result. Here's what I got: [{"Key":{"$content":"hadoken","$type":"System.String"},"Value":{"$content":10,"$type":"System.Int32"}},{"Key":{"$content":"kick","$type":"System.String"},"Value":{"$content":20,"$type":"System.Int32"}},{"Key":{"$content":"fire","$type":"System.String"},"Value":{"$content":30,"$type":"System.Int32"}}] – David TG May 10 '19 at 12:43
  • I'd recommend using NewtonSoft.Json instead, because the default output of that is a lot simpler, and looks exactly like your 2nd attempt. See the answer below from Sir Rufo – Reinstate Monica Cellio May 10 '19 at 12:54
  • Possible duplicate of [C# JSON Serialization of Dictionary into {key:value, ...} instead of {key:key, value:value, ...}](https://stackoverflow.com/questions/4861138/c-sharp-json-serialization-of-dictionary-into-keyvalue-instead-of-keyk) – xdtTransform May 10 '19 at 12:55
  • Btw OT: I really do not recommend using float as version number :) – Erik Šťastný May 10 '19 at 12:56

2 Answers2

2

You can change your POCO to look like this:

public class LevelManager
{
    [JsonProperty("version")]
    public float Version { get; set; }

    [JsonProperty("health")]
    public int Health { get; set; }

    [JsonProperty("powers")]
    public Dictionary <string, int> Powers { get; set; }
}

And use the following code to de-serialize correctly.

private static void Main(string[] args)
{
    string data = @"
    {
            'version': 0.1,
            'health': 100,
            'powers': {
              'Hadoken': 15,
              'Electricity': 20,
              'Rocket': 25,
              'Kick': 20
            }
    }";

    LevelManager level = JsonConvert.DeserializeObject<LevelManager>(data);
}
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
1

Use NewtonSoft.Json and the JSON you get is

{
    "version":1.0,
    "health":100,
    "powers":{
        "Hadoken":15,
        "Electricity":20,
        "Rocket":25,
        "Kick":20
    }
}

A Dictionary<string,int> will be exported as a normal JSON Object with only int properties.

.net fiddle sample

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • Changing EasyJSON (downloaded from Unity3D asset store) to Newtonsoft JSON solved the problem. It was a library issue. – David TG May 10 '19 at 13:49