如何使用JavaScript读取JSON文件

时间:2014-06-05 07:21:45

标签: javascript jquery

我遇到一个问题,我无法弄清楚如何使用test.json阅读JavaScript文件。

Javascript代码:

<script type="text/javascript" src="test.json">
    (function(){
        var nytg = nytg || {};
        $.getJSON("test.json", function(data) {
            nytg.ipoData = data;
            console.log(data);
        });
    }());
</script>

目前使用上面的代码,网站什么也没显示。 这是我的test.json文件。

JSON数据:

 [
   {
    "rMVMP": 46.3313,
    "fulldate": 20140108,
    "NAME": "SBE INC.",
    "rMVOP": 3000.9174,
    "Obs": 1,
    "BHRET3": -77.1
  },
  {
    "rMVMP": 68.357,
    "fulldate": 20140115,
    "NAME": "DICKEY-JOHN",
    "rMVOP": 52.5823,
    "Obs": 2,
    "BHRET3": 0
  },
  {
    "fulldate": 20140121,
    "NAME": "PATHCOM",
    "rMVOP": 677.331
  },
  {
    "rMVMP": 96.247,
    "fulldate": 20140211,
    "NAME": "MANUFACTURING DATA",
    "rMVOP": 76.9979,
    "Obs": 4,
    "BHRET3": 131.479
  },
  {
    "rMVMP": 112.848,
    "fulldate": 20140317,
    "NAME": "CRAY RESEARCH",
    "rMVOP": 89.7345,
    "Obs": 5,
    "BHRET3": 202.941
  },
  {
    "rMVMP": 241.045,
    "fulldate": 20140629,
    "NAME": "SHARED MEDICAL SYSTEMS",
    "rMVOP": 225.539,
    "Obs": 6,
    "BHRET3": 147.28
  },
  {
    "rMVMP": 198.788,
    "fulldate": 20140708,
    "NAME": "DOCUMATION",
    "rMVOP": 200.26,
    "Obs": 7,
    "BHRET3": 28.467
  },
  {
    "rMVMP": 93.507,
    "fulldate": 20140713,
    "NAME": "DATA TERMINAL",
    "rMVOP": 90.4415,
    "Obs": 8,
    "BHRET3": 567.005
  },
  {
    "rMVMP": 656.23,
    "fulldate": 20140812,
    "NAME": "AMDAHL",
    "rMVOP": 659.229,
    "Obs": 9,
    "BHRET3": 99.611
  },
  {
    "rMVMP": 92.331,
    "fulldate": 20140915,
    "NAME": "ROLM",
    "rMVOP": 92.3309,
    "Obs": 10,
    "BHRET3": 819.298
  },
  {
    "rMVMP": 61.2817,
    "fulldate": 20140201,
    "NAME": "CONTINUOS CURVE LENS",
    "rMVOP": 60.5251,
    "Obs": 11,
    "BHRET3": 293.68
  },
  {
    "rMVMP": 64.8688,
    "fulldate": 20140427,
    "NAME": "TESDATA SYSTEMS",
    "rMVOP": 60.2353,
    "Obs": 12,
    "BHRET3": -40.52
  },
  {
    "rMVMP": 200.629,
    "fulldate": 20141214,
    "NAME": "TANDEM COMPUTERS",
    "rMVOP": 159.119,
    "Obs": 13,
    "BHRET3": 753.333
  }
]

1 个答案:

答案 0 :(得分:2)

您正在加载您的json文件,如源文件:

<script type="text/javascript" src="test.json">

该行应为

<script type="text/javascript">

更新:将数据转储到控制台日志中的完整示例:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>D3 example</title>
<style>
</style>

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

</head>
<body>
<script>
    (function(){
        var nytg = nytg || {};
        $.getJSON("24054047-test.json", function(data) {
            nytg.ipoData = data;
            console.log(data);
        });
    }());
</script>
</body>
</html>
wamp服务器上的

console.log(第一个条目):

0: Object
BHRET3: -77.1
NAME: "SBE INC."
Obs: 1
fulldate: 20140108
rMVMP: 46.3313
rMVOP: 3000.9174