0

I am able to convert my nested dictionary to json but in attempting to use Json.Net.JsonNet.Deserialize<SortedDictionary<string, dynamic>>(js) it causes a null reference exception where js is loaded from a file containing: "{"Table":{"RowEntries":{}}}". Not sure what to do from here.

here is code to those it may concern:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    if (openFileDialog1.FileName != "" && openFileDialog1.FileName.EndsWith(".fdb"))
    {
        defaultPath = openFileDialog1.FileName;

        js = @File.ReadAllText(openFileDialog1.FileName);
        Console.WriteLine(js);
        SortedDictionary<string, dynamic> cd;

        try
        {
            cd = Json.Net.JsonNet.Deserialize<SortedDictionary<string, dynamic>>(js);
            DatabaseFunct.currentData.Concat(cd);
            //load tables
            string[] mainTableKeys = DatabaseFunct.GetMainTableKeys();
            foreach (string mainTableKey in mainTableKeys)
            {
                Program.mainForm.tabControl1.TabPages.Add(mainTableKey, mainTableKey);
            }
            //fileName = openFileDialog1.FileName.Remove(openFileDialog1.FileName.Length-4, openFileDialog1.FileName.Length);
            Program.mainForm.label1.Visible = false;
            //triggers event
            Program.mainForm.tabControl1.SelectedIndex = 0;
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
        }
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("no valid file selected!");
    }
}


Edit:

Was using the wrong Json.net package instead of the newtonsoft one.

DED
  • 391
  • 2
  • 12
  • 3
    No one can help you without your code and json – Pavel Anikhouski May 01 '20 at 08:30
  • Does this answer your question? [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – Guru Stron May 01 '20 at 08:33
  • @PavelAnikhouski what code i thought was relevant is included in the question but sure, maybe it's something that went over my head. – DED May 01 '20 at 08:35
  • @GuruStron is `JsonConvert.DeserializeObject()` equivalent to `Json.Net.JsonNet.Deserialize<>()`? i can't seem to call the former from `Json.Net...` i see that there's a class called "JsonConverter" but it doesn't contain "DeserializeObject()"... – DED May 01 '20 at 08:45
  • It is part of [Newtonsoft Json package](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm) – Guru Stron May 01 '20 at 08:49
  • @GuruStron oh, crap, i was using a different "json.net" package. i didn't know there's a specific one out of them that is able to provide this feature. – DED May 01 '20 at 09:02
  • @DED my answer uses the same which you used(based on API), but personally i prefer Newtonsoft's one. – Guru Stron May 01 '20 at 09:11
  • 1
    You have tagged this question [tag:json.net], but Json.NET does not use the `Json.Net` namespace, it uses [`Newtonsoft.Json`](https://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm). And in fact if I attempt to deserialize your JSON to a `SortedDictionary` using the actual Json.NET then it works, see https://dotnetfiddle.net/d4yaWi. So in order for us to help you we need to know what JSON serializer you are actually using -- i.e. a [mcve]. (Or, you could switch to Json.NET). – dbc May 01 '20 at 12:19
  • Not sure whether or not to delete this if someone makes the same mistake with downloading the wrong package that is under a similar name, the answer Guru posted is pretty cool though. I'll just change the tags. – DED May 02 '20 at 01:05

1 Answers1

1

Not sure what you are trying to achieve exactly, but based on provided json this should work:

class MyClass
{
    public dynamic RowEntries { get; set; }
}

JsonNet.Deserialize<Dictionary<string, MyClass>>("{\"Table\":{\"RowEntries\":{}}}")
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Very Interesting! I did some testing and It's like the `"RowEntries":{}` got converted to a class with a dynamic variable that is named after the key of this nested dictionary. But for this to solve my issue i'd want to convert MyClass back into a `KeyValuePair` (as that is what my code expects) and I would not be able to know what the key string is to define this `dynamic RowEntries` variable ahead of time as this does not seem to work if the variable is named anything else. Might just switch to the Newtonsoft package if there's no way around this. – DED May 01 '20 at 10:03
  • 1
    @DED if you are going to use `Newtonsoft`'s package you also can deserialize to [JObject](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm) which can be more suitable for your needs. – Guru Stron May 01 '20 at 12:44