[英]Dapper result to json (using fastjson)
===== UPDATED 8/20/2016 =====
=====更新时间2016年8月20日=====
latest version of fastjson can now handle
Dictionary<string, ?>
type correctly, my problem is solved now.最新版本的fastjson现在可以正确处理
Dictionary<string, ?>
类型,我的问题现在解决了。=============================
=============================
I'm using fastjson to serialize the query result from Dapper, the table in DB has data like this: 我正在使用fastjson来序列化来自Dapper的查询结果,DB中的表具有如下数据:
id | name | price
1 | x | 100
2 | y | 200
....
And when I 当我
using Dapper;
using fastJSON;
// ....
JSON.Parameters.KVStyleStringDictionary = false;
// ....
result = JSON.toJSON(conn.Query("SELECT * FROM tableX"));
I want the result to be: 我希望结果如下:
[{"id":1,"name":"x","price":100},{"id":2,"name":"y","price":200},...]
However the actual result outputs: 但实际结果输出:
[[{"Key":"id","Value":1},{"Key":"name","Value":"x"},{"Key":"price","Value":100}],
[{"Key":"id","Value":2},{"Key":"name","Value":"y"},{"Key":"price","Value":200}]...]
Lots of key-value pairs are generated which looks redundant. 生成了许多看起来多余的键值对。
Is there a way to get the correct result ? 有没有办法得到正确的结果?
Or should I switch to another JSON serializer ? 或者我应该切换到另一个JSON序列化程序?
========== UPDATED ========== ==========更新==========
makubex88's answer indicates that I can create a customized class mapping the table and use conn.Query<myClass>
to get the correct json, though it works for this scenario, it looks like I have to create hundreds of classes for every table in DB to get ideal json result, which is indeed tiring work for me. makubex88的答案表明我可以创建一个映射表的自定义类,并使用
conn.Query<myClass>
来获取正确的json,虽然它适用于这种情况,看起来我必须为DB中的每个表创建数百个类得到理想的json结果,这对我来说确实很累人。 (Thanks any way:P) (谢谢你:P)
Any alternative solutions would be highly appreciated! 任何替代解决方案将受到高度赞赏!
I found a solution to handle it (however it may lose some efficiency), to achieve this, I write my own QueryEx
method, each row in query result is an IDictionary object: 我找到了一个处理它的解决方案(但它可能会失去一些效率),为实现这一点,我编写了自己的
QueryEx
方法,查询结果中的每一行都是一个IDictionary对象:
public IEnumerable<IDictionary> QueryEx(IDbConnection conn, string sql, object argSet = null) {
var result = conn.Query(sql, argSet) as IEnumerable<IDictionary<string, object>>;
return result.Select(r => r.Distinct().ToDictionary(d => d.Key, d => d.Value));
}
And 和
result = JSON.toJSON(conn.QueryEx("SELECT * FROM tableX"));
// output: [{"id":1,"name":"x","price":100},{"id":2,"name":"y","price":200},...]
reason: fastJSON can only parse IDictionary interface correctly, any generic versions of IDictionary will be parsed as Key-Value pair lists 原因:fastJSON只能正确解析IDictionary接口,任何通用版本的IDictionary都会被解析为键值对列表
Try to create a class for your output in JSON then you can serialized it in JSON. 尝试在JSON中为您的输出创建一个类,然后您可以在JSON中对其进行序列化。
//your class
public class Item
{
int ID;
public string Name;
public double Price;
}
//code:
List<Item> = conn.Query<Item>("SELECT * FROM tableX").AsList();
var result = Json(Item, JsonRequestBehavior.AllowGet);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.