使用当前日期读取JSON并使用Javascript获取键值吗?

时间:2018-09-06 04:31:26

标签: javascript

我正试图从键中具有当前日期的JSON响应中获取计数。

Json响应:

[
  {
    "type": {
      "id": "mobile",
      "name": "mobile",
      "description": "",
      "total_count": 0
    },
    "counts": [
      {
        "date": "2018-09-06",
        "timestamp": 1536192000000,
        "count": 20
      },
      {
        "date": "2018-09-07",
        "timestamp": 1536278400000,
        "count": 10
      }
    ]
  },
  {
    "type": {
      "id": "lap",
      "name": "lap",
      "description": "",
      "total_count": 0
    },
    "counts": [
      {
        "date": "2018-09-06",
        "timestamp": 1536192000000,
        "count": 19
      },
      {
        "date": "2018-09-07",
        "timestamp": 1536278400000,
        "count": 20
      }
    ]
  }
]

按照vikscool代码进行的新尝试:

var json_count = JSON.parse(getcounts);
var curDate = getCurrentDate();
var mobilcount = () => json_count.map(ct => {
  const count = ct.counts;
  const getDate = count.find(dt => dt.date === curDate);
  window.alert('count is ', getDate.count);
  return {getDate};
});
mobilcount();

function getCurrentDate () {
  var nowDate = new Date();
  var month = (nowDate.getMonth() + 1).toString().length == 1
    ? '0' + (nowDate.getMonth() + 1)
    : (nowDate.getMonth() + 1);

  var day = nowDate.getDate().toString().length == 1
    ? '0' + nowDate.getDate()
    : +nowDate.getDate();

  return nowDate.getFullYear() + '-' + month + '-' + day;
}

输出:“计数为”

但是没有从json打印出来的计数。

Javascript中是否有解决方案,我可以获取当前日期并获取计数。

我需要将计数设为10的动量计数

1 个答案:

答案 0 :(得分:0)

由于$存储在dates的第一个对象的命名为JSON Array key的{​​{1}}中,因此您无法使用以下方法来获取它:

counts

由于您的json对象没有名为json_count的任何键。

您需要做的是先访问var instance_count=json_count[0].values[2].count; 键,然后从中获取包含特定当前日期为您情况的 ),然后从中获取计数。

以下是获取特定日期计数的示例代码:

values

在上面的代码段中,我创建了两个函数counts以保存在//assuming this is the Json response you are getting as complete JSON response is not provided by the OP. var getcounts = `[{"type":{"id":"mobile","name":"mobile","description":"","total_count":0},"counts":[{"date":"2018-09-05","timestamp":1533686400000,"count":0},{"date":"2018-09-06","timestamp":1533772800000,"count":8}]}]`; //parsing the given json String var json_count = JSON.parse(getcounts); function getCountForCurrentDate() { var curDate = getCurrentDate(); //for which the data is to be fetched and making in format of the dates in JSON as yyyy-mm-dd //finding the particular object that contains the current date var searchedObj = json_count[0]['counts'].find(f => f['date'] == curDate); if(searchedObj!==undefined) console.log('count is', searchedObj['count']); } function getCurrentDate() { var nowDate = new Date(); var month = (nowDate.getMonth() + 1).toString().length == 1 ? '0' + (nowDate.getMonth() + 1) : (nowDate.getMonth() + 1); var day = nowDate.getDate().toString().length == 1 ? '0' + nowDate.getDate() : +nowDate.getDate(); return nowDate.getFullYear() + '-' + month + '-' + day; } getCountForCurrentDate();响应中的格式获取当前日期,并创建了getCurrentDate()以获取{{1} },从JSON变量中获取。

  

更新1 根据新要求由 OP

给出

给定的getCountForCurrentDate()对象如下:

count

现在,由于对象具有两个实体,一个用于json_count,另一个用于JSON,我们可以将特定值提取为:

var json_count = [
  {
    type: {
      id: "mobile",
      name: "mobile",
      description: "",
      total_count: 0
    },
    counts: [
      {
        date: "2018-09-06",
        timestamp: 1536192000000,
        count: 20
      },
      {
        date: "2018-09-07",
        timestamp: 1536278400000,
        count: 10
      }
    ]
  },
  {
    type: {
      id: "lap",
      name: "lap",
      description: "",
      total_count: 0
    },
    counts: [
      {
        date: "2018-09-06",
        timestamp: 1536192000000,
        count: 19
      },
      {
        date: "2018-09-07",
        timestamp: 1536278400000,
        count: 20
      }
    ]
  }
];

现在,为了获得计数,我们这样做:

mobile

,然后以以下方式访问lap

var mobile = json_count.find(f=>f['type']['id']=='mobile');//comparing the value present in the json object at location type.id to 'mobile' (change it to 'lap' or anything that is present in the id of the object).