Can comments be used in JSON?

后端 未结 30 2323
别跟我提以往
别跟我提以往 2020-11-22 02:16

Can I use comments inside a JSON file? If so, how?

30条回答
  •  情书的邮戳
    2020-11-22 02:33

    You can have comments in JSONP, but not in pure JSON. I've just spent an hour trying to make my program work with this example from Highcharts: http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?

    If you follow the link, you will see

    ?(/* AAPL historical OHLC data from the Google Finance API */
    [
    /* May 2006 */
    [1147651200000,67.79],
    [1147737600000,64.98],
    ...
    [1368057600000,456.77],
    [1368144000000,452.97]
    ]);
    

    Since I had a similar file in my local folder, there were no issues with the Same-origin policy, so I decided to use pure JSON... and, of course, $.getJSON was failing silently because of the comments.

    Eventually I just sent a manual HTTP request to the address above and realized that the content-type was text/javascript since, well, JSONP returns pure JavaScript. In this case comments are allowed. But my application returned content-type application/json, so I had to remove the comments.

提交回复
热议问题