如何在没有列名称的情况下序列化sql数据?

时间:2019-07-17 02:16:30

标签: c# asp.net json sql-server-2012

我正在将数据从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视图结构

Image

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

1 个答案:

答案 0 :(得分:0)

这很简单

// 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 fiddle sample


您甚至可以将其填充为

{
  "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 fiddle sample


更新

[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