【问题标题】:web service not returning json as expectedWeb 服务未按预期返回 json
【发布时间】:2013-11-03 22:46:12
【问题描述】:

我有以下网络服务,当通过 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

我使用此服务从我们的数据库中检索 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) 生成 [object Object] 而不是 json 中的实际数据。这是我第一次尝试创建返回 json 的服务,所以如果这是一个简单的任务,请原谅我的无知。

【问题讨论】:

  • 如果你用chrome工具或者firebug调试,data里面的数据是什么样子的?
  • 在提琴手中,它将每个数据数组中的第一项显示为 _type=StockPricesDTO .... 这是导致问题的原因吗?见上面的编辑

标签: jquery json web-services


【解决方案1】:

我会尝试替换

data: '{symbol: "' + symbol + '"}',

data: {symbol: symbol},

如果你这样做,你会看到更多(比使用 alert() 时)

  • 输出到控制台console.log(data)
  • 不要使用 MSIE,而是使用 Chrome、Firefox、...及其 Web-Developer 实用程序
  • 查看 Chrome Webinspector/Firefox Firebug 的网络选项卡并分析请求和响应

在控制台(或调试器)中,您可以检查服务器返回的内容,如果您不同意,请告诉我们。

【讨论】:

  • 0: Object High: 1036 LastSale: 1027.04 Low: 1025.1 Open: 1031.79 QuoteDate: 1383282000000 Volume: 1283300 __type: "StockPricesDTO" 是 chrome 控制台显示的回报
  • 如果 highchart 真的需要[[n,n,n],[n,n,n],...] 形式的数据,您必须在服务器端或客户端转换数据。我会在客户端做。如果您可以依靠 ES5,这只是一些 Array.map 调用的问题。你需要帮助吗?我的回答怎么样?有帮助吗?
  • 答案被接受,因为它引导我走上这条路,最终我使用数组列表并返回数据
猜你喜欢
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2017-03-31
  • 1970-01-01
相关资源
最近更新 更多