如何在Javascript中访问下面的对象参数


output = "{"response":200,"message":"Id: 123 updated successfully"}{"response":403,"message":"Receipt with Id: 124 not updated"}"

如何访问上述输出的响应?由于格式无效,无法转换为json

您的输出几乎是有效的,您可以将两个对象放入一个数组中并遍历它们以检查键值'response',例如打印值,如下所示:

output = [{"response":200,"message":"Id: 123 updated successfully"},
{"response":403,"message":"Receipt with Id: 124 not updated"}];
for(let i=0; i<output.length; i++){
if(output[i].hasOwnProperty('response')){
console.log(output[i]['response']);
}
}

输出:

200
403

请注意,为了使变量'output'可迭代,在返回的响应对象周围使用了'[]'括号。

尝试以下操作:

output = `{"response":200,"message":"Id: 123 updated successfully"}{"response":403,"message":"Receipt with Id: 124 not updated"}`
output = output.replaceAll("}", "},");
output = output.substring(0, output.length - 1);
output = "[" + output  + "]"
console.log(output)
console.log(JSON.parse(output) )

相关内容

  • 没有找到相关文章

最新更新