0

I have a MySQL database and I have a JSON column that stores items as follows:

[{"key":"value"},{"key2","value2"},...}

How can I handle this and load into a C # dictionary? I'm getting error converting from string to dictionary

Example of model:

public class Person
{
  string name;
  Dictionary<string, string> itens;
}
1

3 Answers 3

0

You need to first select it. Dapper does not support this out of the box. Then you could use Newtonsoft.JSON (NuGet-Package) and use it's deserializer like so:

myPerson.itens = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

Reference: https://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm

2
0

You have to create a Custom Handler. I've written detailed articles on that subject.

Samples are also available on GitHub:

https://github.com/yorek/dapper-samples

I've used SQL Server as RDBMS, but everything should apply as is to MySQL

-1
Dictionary<string, string> dir = new Dictionary<string, string>();

                    string splitOn = "value";


                    dir = cnn.Query<string, string, KeyValuePair<string, string>>("YOUR_SP", (s, i) => new KeyValuePair<string, string>(s, i), null, null, false, splitOn, null, null)
        .ToDictionary(kv => kv.Key, kv => kv.Value);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.