首页 > 解决方案 > 如何在没有列名的情况下序列化 sql 数据?

问题描述

我正在将数据从 SQL 数据库序列化为 JSON,如何仅序列化没有字符串名称的值或在反序列化之前修剪序列化 JSON 的函数。

我阅读了有关 ScriptIgnoreAttribute 的信息,但没有看到如何将它与我想做的事情联系起来

原始 JSON

​[
    {
        "CODE": "AF",
        "TOTALVALUE": "$23,554,857.27"
    },
    {
        "CODE": "AS",
        "TOTALVALUE": "$38,379,964.65"
    },
    {
        "CODE": "SG",
        "TOTALVALUE": "$24,134,283.47"
    }
]

所需的 JSON

​[
    {
        "AF": "$23,554,857.27"
    },
    {
        "AS": "$38,379,964.65"
    },
    {
        "SG": "$24,134,283.47"
    }
]

SQL 视图结构

图片

我的 SQL 查询返回数据

SELECT [CODE],[TOTALVALUE] FROM [dbo].[vw_BuyersByCountryValue]
enter code here

ASP.NET 中的序列化代码

[WebMethod]
    public void GetBuyersByCountryValue()
    {

        using (PMMCEntities ctx = new PMMCEntities())
        {
            ctx.Configuration.ProxyCreationEnabled = false;
            var qry = ctx.vw_BuyersByCountryValue.ToList();

            var js = new JavaScriptSerializer();
            string strResponse = js.Serialize(qry);
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.AddHeader("content-length", strResponse.Length.ToString(CultureInfo.InvariantCulture));
            Context.Response.Flush();
            Context.Response.Write(strResponse);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }

标签: c#asp.netjsonsql-server-2012

解决方案


这很简单

// data from the query 
// SELECT CODE, TOTALVALUE FROM vw_BuyersByCountryValue
var sqldata = new [] 
{ 
    new { Code = "AF", TotalValue = "$23,554,857.27" },  
    new { Code = "AS", TotalValue = "$38,379,964.65" },  
    new { Code = "SG", TotalValue = "$24,134,283.47" },  
};

var mappeddata = sqldata.Select( r => 
{
    var dict = new Dictionary<string,string>();
    dict[r.Code] = r.TotalValue;
    return dict;
});

var json = JsonConvert.SerializeObject(mappeddata,Formatting.Indented);

内容json

[
  {
    "AF": "$23,554,857.27"
  },
  {
    "AS": "$38,379,964.65"
  },
  {
    "SG": "$24,134,283.47"
  }
]

.net 小提琴示例


您甚至可以将其填充为

{
  "AF": "$23,554,857.27",
  "AS": "$38,379,964.65",
  "SG": "$24,134,283.47"
}

var sqldata = new [] 
{ 
    new { Code = "AF", TotalValue = "$23,554,857.27" },  
    new { Code = "AS", TotalValue = "$38,379,964.65" },  
    new { Code = "SG", TotalValue = "$24,134,283.47" },  
};

var mappeddata = sqldata.ToDictionary(r => r.Code, r => r.TotalValue);
var json = JsonConvert.SerializeObject(mappeddata,Formatting.Indented);

.net 小提琴示例


更新

[WebMethod]
public void GetBuyersByCountryValue()
{
    using (PMMCEntities ctx = new PMMCEntities())
    {
        ctx.Configuration.ProxyCreationEnabled = false;
        var qry = ctx.vw_BuyersByCountryValue.ToList();

        var mapped = qry.Select r => 
        {
            var dict = new Dictionary<string,string>();
            dict[r.CODE] = r.TOTALVALUE;
            return dict;
        });

        string strResponse = Newtonsoft.Json.JsonConvert.SerializeObject(mapped);

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-length", strResponse.Length.ToString(CultureInfo.InvariantCulture));
        Context.Response.Flush();
        Context.Response.Write(strResponse);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
}

您需要 NuGet 包Newtonsoft.Json


推荐阅读