次のWebサービスがあります.asp.netの「ブラウザで表示」を介してテストすると、ブラウザのxmlに表示されるデータが取得されます
ウェブサービスはこちら
Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System
Imports System.IO
Imports Newtonsoft.Json
Imports System.Text
<ScriptService()> _
<WebService(Namespace:="BATLDataRetrieval")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
Inherits Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function GetOhlcvData(ByVal symbol As String) As List(Of StockPricesDTO)
Dim tempList As New List(Of StockPricesDTO)
Dim stockData As New ArrayList()
Using ctx As New BATLEntities()
Dim symid As Long = (From sym In ctx.last60dayssymbols Where symbol = sym.symbol Select sym.id).FirstOrDefault()
Dim data = (From ohlcv In ctx.last60daysdata
Where ohlcv.lastSixty_symbolId = symid
Select ohlcv).ToList()
For Each dataDay In data
Dim tempSp As New StockPricesDTO
tempSp.QuoteDate = DateTimeToUnixTimestamp(dataDay.date)
tempSp.Open = dataDay.open
tempSp.High = dataDay.high
tempSp.Low = dataDay.low
tempSp.LastSale = dataDay.last
tempSp.Volume = dataDay.volume
tempList.Add(tempSp)
Next
Return tempList
End Using
End Function
このサービスを使用して、db から stockdata を取得し、次のように使用します。この形式の json でデータを返す必要があります。
[[1162512000000,79.36,79.53,77.79,78.29,15426335],
[1162771200000,78.95,80.06,78.43,79.71,15525782],
[1162857600000,80.45,81.00,80.13,80.51,18788494]....]
フィドラーごとに返される実際のjsonは
{"d": [{"__type":"StockPricesDTO","QuoteDate":1383282000000,"Open":1031.79,"High":1036.00,"Low":1025.10,"LastSale":1027.04,"Volume":1283300},{"__type":"StockPricesDTO","QuoteDate":1383195600000,"Open":1028.93,"High":1041.52,"Low":1023.97,"LastSale":1030.58,"Volume":1616400},{"__type":"StockPricesDTO","QuoteDate":1383109200000,"Open":1037.43,"High":1037.51,"Low":1026.00,"LastSale":1030.42,"Volume":1324100},
JavaScriptコードは
$(function () {
var symbol = "GOOG"; //will replace with <a> tag click value
$.ajax({
type: "POST",
url: "WebService.asmx/GetOhlcvData",
contentType: "application/json; charset=utf-8",
data: '{symbol: "' + symbol + '"}',
cache: false,
dataType: "json",
success: function (data) {
alert(data.d);
showChart(data.d);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ' - ' + errorThrown);
}
});
function showChart(chartData) {
$('#chartContainer').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: symbol + ' Stock Price'
},
series: [{
name: symbol,
data: chartData,
tooltip: {
valueDecimals: 2
}
}]
});
}
});
alert(data.d) は、json の実際のデータの代わりに [object Object] を生成します。これは、json を返すサービスを作成する最初の試みです。これが単純な作業である場合は、私の無知を許してください。