0

我有一个如下所示的 JSON 字符串:

{
  "resultType" : "history",
  "currentTime" : "2011-10-22T15:46:00+00:00",
  "columns" : ["date","orders","quantity","low","high","average"],
  "rowsets" : [
    {
      "generatedAt" : "2011-10-22T15:42:00+00:00",
      "rows" : [
        ["2011-12-03T00:00:00+00:00",40,40,1999,499999.99,35223.50],
        ["2011-12-02T00:00:00+00:00",83,252,9999,11550,11550]
      ]
    }
  ]
}

每次我尝试解析它时,我都会使用这样的代码:

var data = JSON.parse(json);
console.log(data);

以下是打印到控制台的内容:

{
  "resultType" : "history",
  "currentTime" : "2011-10-22T15:46:00+00:00",
  "columns" : ["date","orders","quantity","low","high","average"],
  "rowsets" : [
    {
      "generatedAt" : "2011-10-22T15:42:00+00:00",
      "rows" : [Object]
    }
  ]
}

我已经尝试了几件事,但是如何获取现场数据rows?解析后,控制台只显示[Object].

4

2 回答 2

9

您看到的输出就是它的显示方式。如果访问data.rowsets[0].rows,可以看到 JSON 确实解析成功。您还可以util.inspect()在指定depth属性时使用,以告诉 Node.js 在格式化对象时进行更深层次的递归。

这是一个使用示例util.inspect()

var data = JSON.parse(json);
// a null depth means to recurse indefinitely
console.log(util.inspect(data, { depth: null }));
于 2013-09-24T01:23:43.947 回答
0

已测试您的代码。没关系。

   var a = {
      "resultType" : "history",
      "currentTime" : "2011-10-22T15:46:00+00:00",
      "columns" : ["date","orders","quantity","low","high","average"],
      "rowsets" : [
        {
          "generatedAt" : "2011-10-22T15:42:00+00:00",
          "rows" : [
            ["2011-12-03T00:00:00+00:00",40,40,1999,499999.99,35223.50],
            ["2011-12-02T00:00:00+00:00",83,252,9999,11550,11550]
          ]
        }
      ]
    }

    var util = require('util');

    console.log(util.inspect(a.rowsets[0].rows, { showHidden: true, depth: null }));
于 2013-09-24T01:38:54.797 回答