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;
}
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
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);
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.