我有3个选择这样。我已经在sql中将所有3个选择转换为json。
public IHttpActionResult GetTest()
{
string sql = $"SELECT 'Ahmad' AS N'Name',\n" +
"1 AS N'Code',\n" +
"0914 AS N'Phone' FOR JSON PATH\n" +
"SELECT 'Alizadeh' AS N'NameFamily',\n" +
"2 AS N'Code',\n" +
"0915 AS N'Phone' FOR JSON PATH\n" +
" \n" +
" SELECT 'Seven' AS N'BookName',\n" +
"3 AS N'Qty',\n" +
"0916 AS N'IBS' FOR JSON PATH";
var info = Connections.QueryMultiple(sql);
return Ok(info);
}
结果如下:
[
{
"Name": "Ahmad",
"Code": 1,
"Phone": 914
},
{
"NameFamily": "Alizadeh",
"Code": 2,
"Phone": 915
},
{
"BookName": "Seven",
"Qty": 3,
"IBS": 916
}
]
答案 0 :(得分:1)
全部阅读。
public IHttpActionResult GetTest()
{
string sql = $"SELECT 'Ahmad' AS N'Name',\n" +
"1 AS N'Code',\n" +
"0914 AS N'Phone' FOR JSON PATH\n" +
"SELECT 'Alizadeh' AS N'NameFamily',\n" +
"2 AS N'Code',\n" +
"0915 AS N'Phone' FOR JSON PATH\n" +
" \n" +
" SELECT 'Seven' AS N'BookName',\n" +
"3 AS N'Qty',\n" +
"0916 AS N'IBS' FOR JSON PATH";
var info = Connections.SaleBranch.SqlConn.QueryMultiple(sql);
var lstResult = new List<dynamic>();
var isNext = false;
do{
var first2 = info.Read<dynamic>().Single();
lstResult.Add(first2);
isNext=info.IsConsumed;
}
while (!isNext);
return Ok(lstResult);
}
答案 1 :(得分:0)
如果您使用Azure / SQL2016或更高版本,则可以使用
FOR JSON 看到这篇文章: https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server