Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# deserialize Json unknown keys

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?

like image 920
ThePHPAddicted Avatar asked Jul 16 '16 12:07

ThePHPAddicted


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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);
}
like image 70
TSnake41 Avatar answered Oct 24 '22 06:10

TSnake41


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());
like image 1
Tommaso Scalici Avatar answered Oct 24 '22 08:10

Tommaso Scalici