我无法在webstorm的量角器中从JSON对象提取字符串。我想要statusCode的值,即:200,我为此尝试过的代码在下面提到...
代码为:
console.log('body is :' + JSON.stringify(body));
var jsonResponse = JSON.stringify(response);
console.log('---------response is: '+ jsonResponse);
var statusResponse = jsonResponse.getString("statusCode");
console.log('---- statusResponse is :'+ statusResponse.toString());
输出是:
body is :{"oBody":{"payLoad":{"sLoginId":"HDB_TW_DSA@softcell.com","iInstId":4019,"sFirstName":"PRIYANKA","sLastName":"MADGUNDI","sEmail":"priyanka@softcell.com","sEmpId":"HDB18458","sMobile":"8237276692","bActive":true,"aRoles":["DSA"],"aDealers":[{"id":"TW423684019","iInstId":4019,"iDlrId":"42368","sDlrName":"GODSPEED MOTORCYCLE PVT LTD","iBranchId":1278,"sBranchName":"HYDERABAD-SF","sProduct":"TW","bActive":true},{"oStatus":{"iStatus":200,"sStatus":"OK"}}
---------响应为:
{"statusCode":200,"body":{"oBody":{"payLoad":{"sLoginId":"HDB_TW_DSA@softcell.com","iInstId":4019,"sFirstName":"PRIYANKA","sLastName":"MADGUNDI","sEmail":"priyanka@softcell.com","sEmpId":"HDB18458","sMobile":"8237276692","bActive":true,"aRoles":["DSA"],"aDealers","path":"/gonogo-api/atm/login-web-v3","href":"https://ssg.serviceurl.in/login-web-v3"},"method":"POST","headers":{"Postman-Token":"7aad048f-f8ad-4765-a0b8-be3b3a485ab6","cache-control":"no-cache","Content-Type":"application/json","accept":"application/json","content-length":380}}}
答案 0 :(得分:0)
尝试以下选项
var res= JSON.parse(response);
console.log(res[0].statusCode);
希望它对您有帮助
答案 1 :(得分:0)
因此看来jsonResponse从字符串化中给您的内容未被json解析识别为完美的json。因此,您可以自己进行解析,方法是找到提示“ statusCode:”的位置,并从超出该位置的空间获取响应字符串,直到statusCode可以,然后再输入一些(对我来说8个字符就足够了),然后最多逗号。我以为您想要的代码是数字,而不是字符串,所以我将其转换了。
var temp =jsonResponse.search('"statusCode":')+'"statusCode":'.length;
var temp2 = jsonResponse.slice(temp,temp+8);
var status = parseInt(temp2.slice(0,temp2.search(',')),10);