簡體   English   中英

JSON.Net中的JSON序列化在需要數組時給我對象

[英]JSON Serialization in JSON.Net giving me Object when I need an Array

我正在嘗試通過來自ASP.Net的javascript調用填充Highcharts圖表,我正在使用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.Net代碼隱藏...

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看來,您需要聲明的類定義應該類似於以下內容(請參見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()或輔助方法之類的方法。

如果您不關心oreder(我認為您確實如此),也可以利用一些反射來為您完成此操作,但這就是它開始變得更多...丑陋的... :)

我無法從您的代碼中確切得知,但是您可能正在嘗試序列化List<Category>IEnumerable<Category>或類似的東西。 這就是為什么獲得[{"category": "xxxxxx"}]構造的原因。

解決方案:相反,首先將其轉換為字符串數組: string[] ,然后序列化該數組。 這應該導致一個字符串數組,沒有對象。

這里的其他答案充滿了好主意,有些甚至是解決此問題的更好方法……但這是我提出的問題的基本解決方案。

我改變了這個...

   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);

該更改消除了序列化后使用字符串操作的需要,因為我明確地序列化了Array而不是Object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM