我有一個來自一個HTTP請求以下JSON字符串:JSON解析對象
{ '{firstname:\'Joe\'}': '' } // output of console.log(req.body);
我試圖打印的價值利用到控制檯:
console.log(req.body.firstname);
,但它說,該值未定義。我怎樣才能得到名字的價值?
要了解客戶是這樣做的是如何發送JSON請求:
//angular2
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://192.168.1.45:3000/test',
JSON.stringify({firstname:'Joe'}), //This is the parameter I want
{headers:headers}
)
它似乎沒有驗證JSON –
'{firstname:\'Joe \'}'是對象的'key' .. – Rayon
您顯示的對象已被解析爲JavaScript對象或者它是無效的JSON串。除此之外,':'之前的部分是鍵(所以你的鍵是'{firstname:\'Joe''')。它表明答覆已經是錯誤的,或者你在做出錯誤的事情。 –