Dapper result for json (using fastjson)

===== UPDATED 8/20/2016 =====

The latest version of fastjson can now handle the type correctly Dictionary<string, ?>, my problem is resolved now.

===============================

I use fastjson to serialize the query result from Dapper, the table in DB has this data:

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 results of the result:

[[{"Key":"id","Value":1},{"Key":"name","Value":"x"},{"Key":"price","Value":100}],
[{"Key":"id","Value":2},{"Key":"name","Value":"y"},{"Key":"price","Value":200}]...]

Many key-value pairs are created that look redundant.

Is there a way to get the right result?

Or do I need to switch to another JSON serializer?

=========== UPDATED ===========

makubex88 , , , conn.Query<myClass>, json, , , json, . ( : P)

!

+4
2

( ), 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));
}

result = JSON.toJSON(conn.QueryEx("SELECT * FROM tableX"));
// output: [{"id":1,"name":"x","price":100},{"id":2,"name":"y","price":200},...]

: fastJSON IDictionary, IDictionary "-"

+4

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);
+2

All Articles