我有一个MySQL数据库,我有一个JSON列,存储项目如下:
[{"key":"value"},{"key2","value2"},...}
如何处理这个并加载到C#字典中?我收到从字符串转换为字典的错误
型号示例:
public class Person
{
string name;
Dictionary<string, string> itens;
}
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);
您需要先选择它。 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
您必须创建自定义处理程序。我写过关于这个主题的详细文章。
样品也可以在GitHub上找到:
https://github.com/yorek/dapper-samples
我使用SQL Server作为RDBMS,但一切都应该适用于MySQL