我有一个来自一个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''')。它表明答复已经是错误的,或者你在做出错误的事情。 –