JSON JSON序列化.Net在我需要数组时给我对象

本文关键字:JSON 对象 数组 序列化 Net | 更新日期: 2023-09-27 18:17:50

我试图通过ASP的javascript调用填充Highcharts图表。Net,我使用JSON。Net为图表序列化数据。然而,我无法获得从我的数据源创建的JSON来匹配Highcharts所要求的格式。要查看问题,您只需要检查X轴(类别),例如…

[{'"category'":'"August 15 and 16, 2014'"},{'"category'":'"March 21st, 2014'"},{'"category'":'"January 17 and 18, 2014'"},{'"category'":'"August 16 and 17, 2013'"},{'"category'":'"March 22nd, 2013'"},{'"category'":'"January 18 and 19, 2013'"},{'"category'":'"August 17 and 18, 2012'"},{'"category'":'"March 16th, 2012'"},{'"category'":'"January 20 and 21, 2012'"},{'"category'":'"August 19 and 20, 2011'"},{'"category'":'"January 21 and 22, 2011'"}]

应该是这样的…

['August 15 and 16, 2014', 'March 21st, 2014', 'January 17 and 18, 2014', 'August 16 and 17, 2013', 'March 22nd, 2013', 'January 18 and 19, 2013', 'August 17 and 18, 2012', 'March 16th, 2012', 'January 20 and 21, 2012', 'August 19 and 20, 2011', 'January 21 and 22, 2011']

本质上,我的序列化就是创建一个对象列表,而我所需要的只是一个值数组。要解决这个问题,我要么需要生成一个值数组,要么让Highcharts构造函数读取对象。

ASP。网后台代码…

var tblNormal = Reporting.GetHistoricalTicketSalesReport();
    var queryX = from row in tblNormal.AsEnumerable()
        select new
        {
            category = row.ShowDateDescription
        };
    JObject o = JObject.FromObject(new
    {
        categories = queryX
    });
    string strXJSON = o.ToString();
    // value is: "{'"categories'":[{'"category'":'"August 15 and 16, 2014'"},{'"category'":'"March 21 and 21, 2014'"},{'"category'":'"January 17 and 18, 2014'"},{'"category'":'"August 16 and 17, 2013'"},{'"category'":'"March 22 and 22, 2013'"},{'"category'":'"January 18 and 19, 2013'"},{'"category'":'"August 17 and 18, 2012'"},{'"category'":'"March 16 and 16, 2012'"},{'"category'":'"January 20 and 21, 2012'"},{'"category'":'"August 19 and 20, 2011'"},{'"category'":'"January 21 and 22, 2011'"}]}"
    var queryY = from row in tblNormal.AsEnumerable()
        select new HighChartsPoint
        {
            y = row.TicketsSold
        };
    o = JObject.FromObject(new
    {
        series = queryY
    });
    string strYJSON = o.ToString();
    //This removes the wrapper around the inner JSON data
    strXJSON = F.AllAfter(strXJSON, ":").TrimEnd('}');
    //value is: "[{'"category'":'"August 15 and 16, 2014'"},{'"category'":'"March 21 and 21, 2014'"},{'"category'":'"January 17 and 18, 2014'"},{'"category'":'"August 16 and 17, 2013'"},{'"category'":'"March 22 and 22, 2013'"},{'"category'":'"January 18 and 19, 2013'"},{'"category'":'"August 17 and 18, 2012'"},{'"category'":'"March 16 and 16, 2012'"},{'"category'":'"January 20 and 21, 2012'"},{'"category'":'"August 19 and 20, 2011'"},{'"category'":'"January 21 and 22, 2011'"}]"
    strYJSON = F.AllAfter(strYJSON, ":").TrimEnd('}');
    //Call the function on the client browser
    ExecuteJavascript("InitShowChart(" + strXJSON + ", " + strYJSON + ");");

JSON JSON序列化.Net在我需要数组时给我对象

从你的json,似乎你需要声明的类定义应该是这样的(见http://json2csharp.com/):

public class Title
{
    public string text { get; set; }
    public int x { get; set; }
}
public class Subtitle
{
    public string text { get; set; }
    public int x { get; set; }
}
public class XAxis
{
    public List<string> categories { get; set; }
}
public class PlotLine
{
    public int value { get; set; }
    public int width { get; set; }
    public string color { get; set; }
}
public class YAxis
{
    public Title title { get; set; }
    public List<PlotLine> plotLines { get; set; }
}
public class Tooltip
{
    public string valueSuffix { get; set; }
}
public class Legend
{
    public string layout { get; set; }
    public string align { get; set; }
    public string verticalAlign { get; set; }
    public int borderWidth { get; set; }
}
public class Series
{
    public string name { get; set; }
    public List<double?> data { get; set; }
}
public class HighChartsData
{
    public Title title { get; set; }
    public Subtitle subtitle { get; set; }
    public XAxis xAxis { get; set; }
    public YAxis yAxis { get; set; }
    public Tooltip tooltip { get; set; }
    public Legend legend { get; set; }
    public List<Series> series { get; set; }
}

如果声明变量

var data = new HighChartsData();

填充其属性并序列化为

var json = JsonConvert.SerializeObject(data);

您的json将准备好。无需手动创建json

首先,您可以使用NewtonSoft JSON。他很有见地,经常维护他的图书馆。

第二,这里有一个例子。让我们假设一个示例类:

public class AnExample{
    public int AnInt{get;set;}
    public string AString {get;set;}
    public DateTime ADate { get; set; }
    public override string ToString() {
        return "['" + AnInt + "','" + AString +"','"+ADate.ToString("s")+"']";
    }
}

你可以这样写:

void Main()
{
    var example = new AnExample() {
          AnInt = 1
        , AString = "Hello"
        , ADate = DateTime.Now
    };
    var examlpe_as_json = Newtonsoft.Json.JsonConvert.SerializeObject(example);
    // -> {"AnInt":1,"AString":"Hello","ADate":"2014-08-13T16:21:56.8348626+10:00"} 
    var input_to_highchart = example.ToString();
    // -> ['1','Hello','2014-08-13T16:21:56']       
}
当你需要将字符串反序列化回一个类时,Json是有用的…但你想要的似乎是你的图表的输入。我不认为有什么办法可以写像上面的ToString()或helper方法。

如果你不关心顺序(我认为你关心),你也可以利用一些反射来为你做这件事,但这是它开始变得更…丑……:)

我不能从你的代码确切地告诉,但你可能试图序列化一个List<Category>IEnumerable<Category>或类似的东西。这就是为什么你得到[{"category": "xxxxxx"}]结构。

解决方案:相反,首先将其转换为字符串数组:string[],然后序列化该数组。这应该会得到一个字符串数组,没有对象。

这里的其他答案充满了好主意,有些甚至是更好的方法…但这是我提出的问题的基本解决办法。

I changed this…

   var queryX = from row in tblNormal.AsEnumerable()
    select new
    {
        category = row.ShowDateDescription
    };
这个…

var queryX = from row in tblNormal.AsEnumerable()
                 select row.ShowDateDescription;

修复了主要问题,显然在我缺乏LINQ经验的情况下,我显式地声明了一个对象集合。新版本只序列化为一个值列表。

用同样的方法,我也改变了这个....

o = JObject.FromObject(new
{
    series = queryY
});
string strYJSON = o.ToString();
strYJSON = F.AllAfter(strYJSON, ":").TrimEnd('}');
这个…

string strYJSON = JArray.FromObject(queryY).ToString(Newtonsoft.Json.Formatting.None);

这个改变消除了序列化之后使用字符串操作的需要,因为我显式地序列化了一个数组而不是一个对象。