问题描述
我是Newtonsoft的新手。Json和Dapper。
我正在执行一个SQL查询,并使用查询的结果将其转换为JSON字符串,以使其看起来像这样:
{ "Orders" : [{"OrderID":10248,"Quantity":12},{"OrderID":10343,"Quantity":4},...etc...]}
但是,当我运行C#代码时,我的输出看起来完全不同,还有一些意外的添加:
[
{
"JSON_F52E2B61-18A1-11d1-B105-00805F49916B": "{\"Orders\":[{\"OrderID\":10248,\"Quantity\":12},{\"OrderID\":10248,\"Quantity\":10}{\"OrderID\":10271,\"Quantity\":24},{\"OrderID\":10272,\"Quantity\":6},{\"OrderID\":1027"
},{
"JSON_F52E2B61-18A1-11d1-B105-00805F49916B": "2,\"Quantity\":40},...etc... ]
如您所见,我不明白为什么要添加额外的“ JSON_F52E2B61-18A1-11d1-B105-00805F49916B”。如何删除这些?如何更改代码以使其看起来像所需的输出json字符串?
这是我的代码。我还弄错了https://dotnetfiddle.net/uWV6vs的错误输出:
// Dapper Plus
// Doc: https://dapper-tutorial.net/query
// @nuget: Dapper -Version 1.60.6
using Newtonsoft.Json;
using Dapper;
using System;
using System.Data.SqlClient;
public class Program
{
public class OrderDetail
{
public int OrderDetailID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public int Quantity { get; set; }
}
public static void Main()
{
string sql = "SELECT OrderID,Quantity FROM OrderDetails FOR JSON PATH,root ('Orders'),INCLUDE_NULL_VALUES";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
dynamic orderDetail = connection.Query(sql);
//edit: the answer is to use connection.Query<string>,orderDetail[0]
orderDetail = JsonConvert.SerializeObject(orderDetail,Formatting.Indented);
Console.WriteLine(orderDetail);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)