Highcharts,Newtonsoft.Json和ASP.NET

时间:2014-02-27 20:53:00

标签: c# asp.net json vb.net highcharts

我在json中有我的MS SQL数据。我使用Newtonsoft.Json。 我的json我用这个;

> Dim json As String = JsonConvert.SerializeObject(dt, New
> Newtonsoft.Json.Formatting())

我的json看起来像这样;

> {"Id":16846,"AXA":2.2090,"BXB":2.2270,"HMS":"08:17:05"},
{"Id":16847,"AXA":2.2091,"BXB":2.2271,"HMS":"08:17:21"},
{"Id":16848,"AXA":2.2087,"BXB":2.2271,"HMS":"08:18:02"},
{"Id":16852,"AXA":2.2090,"BXB":2.2270,"HMS":"08:42:00"},

在asp.net aspx页面中我想要分类:以此显示时间;

  

08:17:05,08:17:21,08:18:02,08:42:00等等......

我希望我的2系列看起来像这样;

>     series: [{
>               name: 'BUY',
>               data: [2.2090, 2.2091, 2.2087, 2.2090]
>                     }, {
>               name: 'SELL',
>               data: [2.2270, 2.2271, 2.2271, 2.2270]
>     }]

我的问题是如何将数据分类以显示时间以及如何将数据分成两个系列。我很难将数据发送到aspx部分。

我找到了这段代码,但我不知道如何应用我的jason来形成我想要的格式。

>     dLen = json.length,
>        ret = [];
>        for (var i = 0; i < dLen; i++) {
>         ret.push(parseInt(d[i].x, 10));}

感谢您阅读我的帖子,希望我能获得一些详细信息,以便我可以动态构建我的第一个asp.net高清。

亲切的问候,

谢谢梅森。我不确定我是否正确,但我创建了序列化为;

Imports Microsoft.VisualBasic
Imports System.Runtime.Serialization

Public Class SerializedJsonDT
    Private mstrLine As String

    '{"Id":16846,"AXA":2.2090,"BXB":2.2270,"HMS":"08:17:05"}
    'This was my json output. And I will change the output with
    'this Serialization prosses. Then I will pass this class to json.

    <DataContract> _
    Public Class [MyClass]

        'CURRENCY: BUY Data
        <DataMember> _
        Public Property WillSerializeKurBuy() As Decimal
            Get
                Return m_WillSerializeKurBuy
            End Get
            Set(value As Decimal)
                m_WillSerializeKurBuy = value
            End Set
        End Property
        Private m_WillSerializeKurBuy As Decimal

        'CURRENCY: SELL Data
        <DataMember> _
        Public Property WillSerializeKurSell() As Decimal
            Get
                Return m_WillSerializeKurSell
            End Get
            Set(value As Decimal)
                m_WillSerializeKurSell = value
            End Set
        End Property
        Private m_WillSerializeKurSell As Decimal

        'CURRENCY: TIME Categories
        <DataMember> _
        Public Property WillSerializeKurTarih() As String
            Get
                Return m_WillSerializeKurTarih
            End Get
            Set(value As String)
                m_WillSerializeKurTarih = value
            End Set
        End Property
        Private m_WillSerializeKurTarih As String

    End Class

End Class

现在你提到我将在服务器端创建一个函数,但我不知道如何从函数发送dt(datatable)到这个类。我感谢你的回应,并向我展示了正确的方向。我想我正在一步一步地实现目标。我希望你能告诉我或指点我一个方向。我需要你更多的帮助。 :)

亲切的问候,

2 个答案:

答案 0 :(得分:0)

你可以用这种方式解析你的json

var categories = [],
    series = [
        {
            name: "BUY",
            data: []
        },{
            name: "SELL",
            data: []
        }
    ];

var json = [{
    "Id": 16846,
    "AXA": 2.2090,
    "BXB": 2.2270,
    "HMS": "08:17:05"
}, {
    "Id": 16847,
    "AXA": 2.2091,
    "BXB": 2.2271,
    "HMS": "08:17:21"
}, {
    "Id": 16848,
    "AXA": 2.2087,
    "BXB": 2.2271,
    "HMS": "08:18:02"
}, {
    "Id": 16852,
    "AXA": 2.2090,
    "BXB": 2.2270,
    "HMS": "08:42:00"
}];

$.each(json, function (i, p){
    series[0].data.push(p.AXA);
    series[0].data.push(p.BXB);
    categories.push(p.HMS);
});

示例:http://jsfiddle.net/RJ9Bq/

答案 1 :(得分:0)

这是在C#中,因为我不熟悉VB.NET。但是,如果需要,您可以自动翻译代码。

public class MyJsonFriendlyClass()
{
    public string name {get;set;}
    public List<int> data {get;set;}

    public MyJsonFriendlyClass()
    {
        data=new List<int>();
    }

    public static List<MyJsonFriendlyClass> GetJsonFriendlyClasses(DataTable dt)
    {
         List<MyJsonFriendlyClass> returndata=new List<MyJsonFriendlyClass>();
         MyJsonFriendlyClass buy=new MyJsonFriendlyClass();
         buy.name="BUY";
         MyJsonFriendlyClass sell=new MyJsonFriendlyClass();
         sell.name="SELL";
         foreach(DataRow dr in dt)
         {
             buy.data.Add(dr["AXA"]);//you may have to convert based on data type of your column
             sell.data.Add(dr["BXB"]);
         }
         returndata.Add(buy);
         returndata.Add(sell);
         return returndata;      
    }
}

用法

List<MyJsonFriendlyClass> classes=GetJsonFriendlyClasses(dt);
string json = JsonConvert.SerializeObject(classes);

我们正在做的是创建一个类,当序列化为JSON时,它将匹配您想要结束的数据结构。然后我们从dt(我假设是一个DataTable)到我们新类的列表。我们将结果列表序列化,并且我假设您将其返回给客户端以便在JavaScript中进行处理。