簡體   English   中英

訪問JSON對象中的值

[英]Accessing values in JSON object

我有一個JSON對象event.body ,其中包含{"article_url": "http://technewstt.com/bd1108/"}如何訪問article_url的值,換句話說我想使用字符串http://technewstt.com/bd1108/
我嘗試了event.body.article_urlevent.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;

使用body-parser中間件:

const bodyParser = require('body-parser');

app.use(bodyParser.json());

那你應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM