简体   繁体   English

从 JSON object 中提取特定数据

[英]Pulling particular data from a JSON object

Currently, I am using substring() and lastIndexOf() to extract the data I need from a given JSON object.目前,我正在使用 substring() 和 lastIndexOf() 从给定的 JSON object 中提取我需要的数据。 However, sometimes the JSON object's order of the data changes and as such messes up the data I retrieve.但是,有时 JSON 对象的数据顺序会发生变化,因此会弄乱我检索的数据。

I am looking for a more exact and precise solution of pulling example@live.com and ExampleName both without quotes from a given jwt token as seen below through the use of whatever it may be to accomplish it.我正在寻找一个更精确和精确的解决方案来拉取 example@live.com 和 ExampleName 两者都没有来自给定 jwt 令牌的引号,如下所示,通过使用任何可能的方式来完成它。 Thank you.谢谢你。

{"user":{"_id":"5ec0e2d36851fcd7ab20a0fe","email":"example@live.com","name":"ExampleName","highScore":0},"iat":1592025639,"exp":1592025939}

why do you use substring?你为什么使用 substring? Your token is string?你的令牌是字符串? Then just convert it to object and read its properties然后只需将其转换为 object 并读取其属性

const obj = JSON.parse(yourToken)
const { name, email } = obj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM