我有一个MySQL数据库和一个JSON列,它按如下方式存储项目:
[{"key":"value"},{"key2","value2"},...}
我如何处理这个问题并加载到C#字典中?从字符串转换为字典时出现错误
模型示例:
public class Person
{
string name;
Dictionary<string, string> itens;
}
发布于 2018-06-26 20:38:30
您需要首先选择它。Dapper does not support this out of the box。然后您可以使用Newtonsoft.JSON (NuGet-Package),并像这样使用它的反序列化程序:
myPerson.itens = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
参考:https://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm
发布于 2018-06-26 22:34:10
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);
发布于 2018-06-28 05:04:10
您必须创建自定义处理程序。我已经写了关于这个主题的详细文章。
GitHub上也提供了示例:
https://github.com/yorek/dapper-samples
我已经使用SQL Server作为关系型数据库管理系统,但是一切都应该适用于MySQL
https://stackoverflow.com/questions/51043112
复制相似问题