简体   繁体   English

JSON数据格式不正确

[英]Incorrect JSON data format

I am trying to create some JSON to be used for displaying a chart using Highcharts 我正在尝试使用Highcharts创建一些用于显示图表的JSON

http://www.highcharts.com/ http://www.highcharts.com/

I have copied one of their examples: 我复制了他们的一个例子:

http://www.highcharts.com/stock/demo/basic-line http://www.highcharts.com/stock/demo/basic-line

Click "View Options" under the graph to see the source. 点击图表下方的“查看选项”查看来源。 There is also a JSFiddle there to play with 还有一个JSFiddle可供玩耍

If I copy that locally it all works fine. 如果我在本地复制它一切正常。

The problem is when I try to use my own data source. 问题是当我尝试使用自己的数据源时。

I have an ASP.Net MVC controler which is spitting out a list of arrays, just like their data source. 我有一个ASP.Net MVC控制器,它正在吐出一个数组列表,就像它们的数据源一样。 However, that doesn't work. 但是,这不起作用。

Their datasource looks like this http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback= ? 他们的数据源看起来像这样http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=

and they retrieve it like this 他们像这样检索它

$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function (data) {

So I thought I'd take a step back and copy thier data exactly and put it in a text file on my server and try that: 所以我想我退后一步,准确地复制他们的数据并将其放在我的服务器上的文本文件中并尝试:

So I tried this 所以我尝试了这个

$.getJSON('/data.txt', function (data) {

and this 还有这个

$.get('/data.txt', function (data) {

but neither work 但都没有工作

I have also tried using both JSON.parse and jQuery.parseJSON after retrieving the data, but again - that doesn't seem to work 我在检索数据后也尝试过使用JSON.parsejQuery.parseJSON ,但是再次 - 这似乎不起作用

I am also wondering what the ? 我也想知道是什么? is at the start of their data 是他们的数据的开始

Their data looks like this 他们的数据看起来像这样

?([[<some data>],[some data]]);

I don't get any error message, the graph just doesn't display 我没有收到任何错误消息,图表只是不显示

any ideas? 有任何想法吗?

SOLVED IT 解决了它

Just need to retrive the data and turn it into an array and pass it to the chart. 只需要检索数据并将其转换为数组并将其传递给图表。

Needs to be an array, not JSON 需要是一个数组,而不是JSON

That datasource is ouputting JSONP, which is for cross-domain AJAX requests. 该数据源正在输出JSONP,这是针对跨域AJAX请求的。 It's not valid 'raw' JSON because of that extra callback(...) wrapper. 由于额外的callback(...)包装,它不是有效的'原始'JSON。

Read up about it here: http://api.jquery.com/jQuery.ajax/ under the 'dataType' section. 在这里阅读它: http//api.jquery.com/jQuery.ajax/在'dataType'部分下。

As you say in your tags, it's not JSON, it's JSONP. 正如你在标签中所说,它不是JSON,而是JSONP。 Do not parse it, catch it with a callback. 不解析它,用回调捕获它。 Use jQuery.getScript to do it, and define function callback(data) . 使用jQuery.getScript来完成它,并定义function callback(data) Inside that function, data should contain the (parsed) object. 在该函数内部, data应包含(已解析的)对象。 Also, replace the ? 另外,更换? in the URL with callback (or whatever you named your function) - ? callback的URL中(或者你命名为函数的任何东西) - ? is not a valid identifier in JavaScript, so ?([....]) is nonsense. 在JavaScript中不是有效的标识符,所以?([....])是无稽之谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM