8

I have this JSON and i have to deserialize it:

{
  "homepage": "http://files.minecraftforge.net/maven/net/minecraftforge/forge/",
  "promos": {
    "1.10-latest": "12.18.0.2000",
    "1.10.2-latest": "12.18.1.2014",
    "1.10.2-recommended": "12.18.1.2011",
    "1.5.2-latest": "7.8.1.738",
    "1.5.2-recommended": "7.8.1.737",
    "1.6.1-latest": "8.9.0.775",
    "1.6.2-latest": "9.10.1.871",
    "1.6.2-recommended": "9.10.1.871",
    "1.6.3-latest": "9.11.0.878",
    "1.6.4-latest": "9.11.1.1345",
    "1.6.4-recommended": "9.11.1.1345",
    "1.7.10-latest": "10.13.4.1614",
    "1.7.10-latest-1.7.10": "10.13.2.1343",
    "1.7.10-recommended": "10.13.4.1558",
    "1.7.2-latest": "10.12.2.1147",
    "1.7.2-recommended": "10.12.2.1121",
    "1.8-latest": "11.14.4.1577",
    "1.8-recommended": "11.14.4.1563",
    "1.8.8-latest": "11.15.0.1655",
    "1.8.9-latest": "11.15.1.1902",
    "1.8.9-recommended": "11.15.1.1722",
    "1.9-latest": "12.16.0.1942",
    "1.9-recommended": "12.16.1.1887",
    "1.9.4-latest": "12.17.0.1990",
    "1.9.4-recommended": "12.17.0.1976",
    "latest": "12.18.1.2014",
    "latest-1.7.10": "10.13.2.1343",
    "recommended": "12.18.1.2011"
  }
}

Searching on this website a lot, i came out with this code:

dynamic json = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
foreach (KeyValuePair<string, string> entry in json["promos"])
{
    MessageBox.Show(entry.Key);
    MessageBox.Show(entry.Value);
}

I need to get both Key and Value from that Json but with this code it says there is an unexpected character on line 3 pos 13. I tried in a lot of different ways but i can't get value and key at the same time. With some code i got just the key and with some other code i got just the value. Can you explain me how to obtain both value and key?

Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
ThePHPAddicted
  • 303
  • 1
  • 4
  • 16

3 Answers3

9

You can directly parse the json as JObject and convert the node "promos" to Dictionary

var json = JObject.Parse(data);
var promos = json["promos"].ToObject<Dictionary<string, string>>();
foreach (var entry in promos)
{
    MessageBox.Show(entry.Key);
    MessageBox.Show(entry.Value);
}
TSnake41
  • 405
  • 3
  • 8
1

The issue is the type you're trying to deserialize into. You're trying to deserialize it as a Dictionary, but while the first entry is a pair of string, promos is a Dictionary itself, so you can't cast the value of "promos" to string! Supposing your json is in a file called "json.json" you can obtain a Dictionary by iterating through promos JObject and cast the items you obtain to a JProperty, in this way:

var json = File.ReadAllText("json.json");
var deserialized = JsonConvert.DeserializeObject<JObject>(json);

var dictionary = new Dictionary<string, string>();

foreach (JProperty item in deserialized["promos"])
    dictionary.Add(item.Name, item.Value.ToString());
Tommaso Scalici
  • 480
  • 1
  • 6
  • 15
1

Your data is not a Dictionary<string, string> because the value of promos is not a string but a Dictionary.

You could deserialize to an JObject and then get the value of your promos property.

JObject json = JsonConvert.DeserializeObject<JObject>(data);
//var promos = json.Value<Dictionary<string, string>>("promos");
var promos = data["promos"].ToObject<Dictionary<string, string>>();
foreach (KeyValuePair<string, string> entry in promos) {
   //do some stuff
}

EDIT

Not sure, why the original way does not work and don't have the time right now to investigate, but the second line should do it.

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • It says System.InvalidCastException: Cannot cast newtonsoft.json.linq.jobject into newtonsoft.json.linq.jtoken. What does it mean? How can i solve it? – ThePHPAddicted Jul 16 '16 at 13:39