[英]Accessing values in JSON object
我有一個JSON對象event.body
,其中包含{"article_url": "http://technewstt.com/bd1108/"}
如何訪問article_url
的值,換句話說我想使用字符串http://technewstt.com/bd1108/
。
我嘗試了event.body.article_url
和event.article_url
,它們都是undefined
如果您確定event.body
包含正確的數據,您可以使用json解析它並獲取值和鍵
JSON.parse('{"article_url": "http://technewstt.com/bd1108/"}', (key, value) => {
console.log(key); //logs the key which is article_url
return value; // returns the url value
})
這是平等的
JSON.parse(JSON.stringify(event.body), (key, value) => {
console.log(key);
return value;
})
其他選項,如用戶@GrégoryNEUT評論將返回url值
JSON.parse(event.body).article_url //returns the url value
最好的辦法:
var url = JSON.parse(event.body).article_url;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.