首页 > 解决方案 > JS 过滤 JSON 响应

问题描述

不幸的是,我不熟悉 JS,我需要过滤这个字符串,例如:

{
   "code":200,
   "success":true,
   "data":{
      "token":".J1e9ipFZkPE6EvIRAqEf9hp",
      "expires":"2019-01-05 14:18:43"
   },
   "time":0.009
}

很明显我需要获取令牌值,我发现在 XML 中有 jsonpath 就像 Xpath 一样,但我不知道在 JS 中这样做。

我想得到你的帮助

标签: javascriptjsonfilter

解决方案


你是这个意思吗?

const text = '{ "code":200, "success":true, "data":{ "token":".J1e9ipFZkPE6EvIRAqEf9hp", "expires":"2019-01-05 14:18:43" }, "time":0.009 }'

const json = JSON.parse(text);
const token = json.data.token

console.log(token)


推荐阅读