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

使用当前日期读取JSON并使用Javascript获取键值?,javascript,Javascript,我试图从JSON响应中获取计数,该响应的键中有当前日期 Json响应: [ { "type": { "id": "mobile", "name": "mobile", "description": "", "total_count": 0 }, "counts": [ { "date": "2018-09-06", "timestamp": 1536192000000,

我试图从
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
      }
    ]
  }
]
我的新尝试符合Viksool代码:

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
中是否有任何解决方案我可以获取当前日期和计数


我需要在mobilcount中将计数设为10。

由于
日期
存储在
JSON数组键中
名为
计数
JSON\u计数
的第一个对象的
计数中,您无法使用以下方法获取它:

var instance_count=json_count[0].values[2].count;
因为json对象没有任何名为
值的键

您需要做的是首先访问
计数
键,然后从中获取包含特定的特定对象(与您的情况相同的当前日期),然后从中获取计数

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

//假设这是您得到的Json响应,因为OP没有提供完整的Json响应。
var getcounts=`[{“类型”:{“id”:“移动”,“名称”:“移动”,“描述”:““总数”:0}”,计数:[{“日期”:“2018-09-05”,“时间戳”:1533686400000,“计数”:0},{“日期”:“2018-09-06”,“时间戳”:1533772800000,“计数”:8}]`;
//解析给定的json字符串
var json_count=json.parse(getcounts);
函数getCountForCurrentDate(){
var curDate=getCurrentDate();//为其获取数据,并以JSON中的日期格式制作为yyyy mm dd
//查找包含当前日期的特定对象
var searchedObj=json_count[0]['counts'].find(f=>f['date']==curDate);
if(searchedObj!==未定义)
log('count is',searchedObj['count']);
}
函数getCurrentDate(){
var nowDate=新日期();
变量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()++'-'+月+'-'+日;
}

getCountForCurrentDate()
这是完整的
JSON
响应对象吗?如果是,则我无法在其中看到名为
values
的数组。JSON blob无效,并且在任何地方都找不到
values
属性。@Viksool,@Robby,对不起,我的意思是count:Integer(一些int).我需要获取当前数据的计数date@dow更新后的代码几乎正确,只需更改
窗口。alert()
alert()
仅接受1个参数,因此只需更改代码,将字符串与值串联,或将值作为
窗口。alert(“count is”+getDate.count)传递给它即可@vikscoll,这是一个很好的捕获,我真的不知道警报需要一个参数。现在学会了。谢谢如果我想根据JSON中的Id获取计数,还有一个疑问。可以做些什么?我正在尝试类似的东西:var mobilcount=json_count.map(ct=>{const id=ct.type.id['mobile'];const count=ct.counts;const getDate=count.find(dt=>dt.date==curDate);window.alert(“count是”+id.getDate.count);但我得到了错误,因为id未定义。如何获取lap、mobile和tablet Count的id此代码非常棒,但我不确定为什么我得到了未定义的otuput。它不在json中。您能否提供实际的json
响应
或在
getCountForCurrentDate()中设置断点
并检查
searchedObj
的值是多少。我已经用您的尝试编辑了这个问题,并提供了JSON。您能在这里帮助我吗?为什么我没有得到计数