如何在javascript中解析JSON词典列表

时间:2015-10-02 20:39:48

标签: javascript json html5

我是javascript的新手,需要遍历JSON词典列表,为每个键创建一个列表。

这是我的JSON:

[
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Color Correction",
        "Status": "In Progress",
        "Completion": "60"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Conform",
        "Status": "In Progress",
        "Completion": "70"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 2",
        "Job": "Scanning",
        "Status": "Complete",
        "Completion": "100"
    },
    {
        "Date": "Aug. 12, 2015",
        "Reel": "Reel 1",
        "Job": "QC only",
        "Status": "In Progress",
        "Completion": "50"
    }
]

我正在使用此函数读取JSON:

$.getJSON('cgi-bin/fakedata.txt', function(main)

我希望能够访问main['Date'] = ['August 12, 2015','August 14, 2015']

等...

3 个答案:

答案 0 :(得分:0)

这应该有效

 var x = JSON.stringify(json,["date"]);

然后你可以把它变成JSON。

答案 1 :(得分:0)

function fieldValues(main, field) {
  var res = {};
  for(item of main) {
    res[item[field]] = 0;
  }
  return Object.keys(res);
}

var main = [
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Color Correction",
        "Status": "In Progress",
        "Completion": "60"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Conform",
        "Status": "In Progress",
        "Completion": "70"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 2",
        "Job": "Scanning",
        "Status": "Complete",
        "Completion": "100"
    },
    {
        "Date": "Aug. 12, 2015",
        "Reel": "Reel 1",
        "Job": "QC only",
        "Status": "In Progress",
        "Completion": "50"
    }
];

console.log(fieldValues(main, "Date"));

答案 2 :(得分:0)

下面是一个可运行的节点程序。在我看来,你试图把日期变成一个单独的数组。所以你可以像日期[0]那样访问它们。

getTime()