-1

I have this JObject:

[{"Antibiotic after diagnosis": ["Azithromycin", "CeftriaXONE", "Cefpodoxime Proxetil", "Linezolid", "Vancomycin Oral Liquid", "Ciprofloxacin HCl", "Ampicillin Sodium"], "City": ["Tel Aviv", "Beersheba", "Jerusalem", "Haifa"], "Gendercode": ["M", "F", "E"]}]

I wanted to convert this JObject to regular object so i tried using ToObject() method:

Dictionary<string, object> dictObj = new Dictionary<string, object>();
dictObj = myJsonObj.ToObject<Dictionary<string, object>>();

But the code crushed with the folowing error:

JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

How do i convert my JObject to regular Object?

raz erez
  • 79
  • 1
  • 3
  • 12
  • Your JSON is not well-formed. There are commas missing at the end of the `"key0"` and `"key1"` lines. Post your JSON to https://jsonlint.com/ to see for yourself. Can you confirm whether your JSON is really ill-formed, or whether there is a typo in your question? If the latter, can you please [edit] your question and include the actual JSON? – dbc Jul 23 '18 at 09:10
  • After fixing the JSON to add the missing commas, I cannot reproduce your problem. See https://dotnetfiddle.net/wrHJAU for a working demo. For us to help you, we will need to see a [mcve] that reproduces your problem. – dbc Jul 23 '18 at 09:10
  • Why would you be doing this? JObject is already a Dictionary? – Filip Cordas Jul 23 '18 at 09:17
  • _How do i convert my JObject to regular Object?_ And JObject is already an Object. You question makes little sense. – Filip Cordas Jul 23 '18 at 09:22
  • 1
    @FilipCordas - a `JObject` is an `IDictionary` not a `Dictionary`. It's perfectly reasonable to want to convert a `JObject` to a .core .Net framework class like `Dictionary`. See for instance [How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](https://stackoverflow.com/q/5546142/3744182) which shows one way to do it. – dbc Jul 23 '18 at 09:25
  • @dbc You are right is an IDictionary it was a type on my part but this question makes no sense as it stands. The link you posted is a question that makes sense. But he needs to edit the question. – Filip Cordas Jul 23 '18 at 09:37

2 Answers2

0

Try this:

Dictionary<string, object> dictObj = new Dictionary<string, List<string>>();
dictObj = myJsonObj.ToObject<Dictionary<string, List<string>>>();
abydal
  • 378
  • 1
  • 8
0

I think you problem is that you want to desterilize an array to a dictionary. In any case you might want to rethink what you are doing. But here is how you get a Dictionary<string,List<string>>

var json = @"[
{
""Antibiotic after diagnosis"": [""Azithromycin"", ""CeftriaXONE"", ""Cefpodoxime Proxetil"", ""Linezolid"", ""Vancomycin Oral Liquid"", ""Ciprofloxacin HCl"", ""Ampicillin Sodium""], 
""City"": [""Tel Aviv"", ""Beersheba"", ""Jerusalem"", ""Haifa""], 
""Gendercode"": [""M"", ""F"", ""E""]
}]";

            var myJsonObj = JArray.Parse(json);

            Dictionary<string,List<string>> dictObj = myJsonObj.Select(i => 
            i.Cast<JProperty>().ToDictionary(
                k => k.Name, 
                v => v.Value.Select(t => t.Value<string>()).ToList())).ToArray().First();

Also have a look at How do I use JSON.NET to deserialize into nested/recursive Dictionary and List? since as @dbc mentioned this might be a duplicate if that is what you need.

Filip Cordas
  • 2,531
  • 1
  • 12
  • 23