Javascript 如何在Posman中将文本与变量值分离

Javascript 如何在Posman中将文本与变量值分离,javascript,json,parsing,variables,postman,Javascript,Json,Parsing,Variables,Postman,我试图在Postman中将文本和变量值分开 值我的变量: { "id": ObjectID("x1223123x"), "name": "John", "tokens": [{ "type": "activation", "token": "xX-tokenValue" }]} 我抓住了它: cons

我试图在Postman中将文本和变量值分开

值我的变量:

{
"id": ObjectID("x1223123x"),
"name": "John",
"tokens": [{
    "type": "activation",
    "token": "xX-tokenValue"
}]}
我抓住了它:

const$=cheerio.load(pm.response.text())
pm.globals.set(“textMongo”,“$”(“textarea[name*='document'])).text()

我将其存储为文本类型,因为我将其与html页面分离

当我试图将其解析为json时,我遇到了一个问题:

console.log(JSON.parse($( "textarea[name*='document']" ).text()));
JSONError: No value found for key _id at 2:12 "id": ObjectID("x1223123x"),
不管怎样。。。如何将“token”的值存储在新变量中:“xX tokenValue”


在您的例子中,响应是JSON格式的,因此您不需要使用字符串函数来获取令牌。

正如我之前所写的,我的响应是html。我将html的一部分分隔为字符串形式的变量。现在,我想将该变量的值转换为JSON,然后提取标记值。
var res = pm.response.json();
if(res.tokens!=null && res.tokens.lenght>0)
{
  pm.globals.set("textMongo",res.tokens[0].token);
}