[英]parse HttpClientRequest response
i have another novice (and probably stupid) question. 我还有另一个新手(可能是愚蠢的)问题。 i am using HttpClientRequest and making a post call.
我正在使用HttpClientRequest并进行呼叫。 also i have the response.
我也有回应。
var url = <my url>
var request = new HttpClientRequest(url);
request.header["Content-Type"] = "application/x-www-form-urlencoded";
request.method = "POST";
try
{
request.execute();
var rawResponse = request.response.body.toString();
}
the response from server is in the following format: {"token":"abc","expires_in":9292,"refresh":"deeDfTTgendj"} 服务器的响应格式如下:{“ token”:“ abc”,“ expires_in”:9292,“ refresh”:“ deeDfTTgendj”}
i just need to extract "expires_in" and "refresh" fields from the response 我只需要从响应中提取“ expires_in”和“ refresh”字段
Since that is valid JSON, you can parse it: 由于这是有效的JSON,因此可以对其进行解析:
var rawResponse = request.response.body.toString(),
objectLiteral = JSON.parse(rawResponse);
var expires_in = objectLiteral['expires_in'],
refresh = objectLiteral['refresh'];
var rawResponse = '{"token":"abc","expires_in":9292,"refresh":"deeDfTTgendj"}'; objectLiteral = JSON.parse(rawResponse); var expires_in = objectLiteral['expires_in'], refresh = objectLiteral['refresh']; console.log(expires_in, refresh);
Note: check out browser support for JSON.parse()
注意:查看浏览器对
JSON.parse()
支持
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.