简体   繁体   English

在Angular中解码HTTP响应-JSON.parse问题

[英]Decode http response in Angular - JSON.parse issue

After quite a bit of messing around, I've got to the point where I can recieve the following body in a 404 error response from my backend. 经过一番混乱之后,我到达了可以从后端收到404错误响应中的以下正文的地步。 I'm struggling to parse the content into angular so I can use it. 我正在努力将内容解析为有角度的内容,以便可以使用它。 I'm know this is simple stuff, so sorry to ask such a basic question. 我知道这很简单,很抱歉提出这样一个基本问题。 The _body looks like this. _body看起来像这样。

 _body: "{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}"

These are fine: 这些很好:

console.log("Err = ", err);
Err =  Response {_body: " 
{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}", status: 404, ok: false, statusText: "OK", headers: Headers, …}

and: 和:

console.log("Err Body : ", err._body);
Err Body :  
{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}

But this doesn't work: 但这不起作用:

let errorObject = eval(errorString);
Uncaught (in promise): SyntaxError: Invalid or unexpected token

... ...

var errBody = JSON.parse(errorString);
console.log("JS err body", errBody);
Error: Uncaught (in promise): SyntaxError: Unexpected token \ in JSON at position 1

But I can't figure out how to get the individual fields out. 但是我不知道该如何处理各个字段。 I'm aware that the above efforts are naive and wrong. 我知道上述努力是幼稚和错误的。 I'm sure anyone with JS or angular skills can solve this in a minute. 我敢肯定,具有JS或棱角技巧的人都可以在一分钟内解决此问题。

PS cut me some slack. PS让我有些懈怠。 I'm a hardware designer. 我是硬件设计师。 I'm here because I don't know something, which is always the best reason to ask a question. 我在这里是因为我一无所知,这始终是问问题的最佳理由。

Edit: 编辑:

Thanks for the answers. 感谢您的回答。 JSON.parse does not work for me!? JSON.parse对我不起作用!

SyntaxError: Unexpected token \ in JSON at position 1

I looked more closely at what you had success with, and I agree it works fine in the console. 我仔细研究了您的成功做法,并同意它可以在控制台中正常工作。 But it does not work for me in Angular. 但这在Angular中对我不起作用。 What did work was: 起作用的是:

let errBody = JSON.parse("\"" + err._body + "\"");

Although it seems ridiculous to do. 尽管这样做似乎很荒谬。 Especially since, afterwards, the result is not quite right: 尤其是之后,结果并不完全正确:

err body {"httpStatus":404,"errorType":"NotFound","message":"Device does not exist!"}

If I then try to get at errBody.message, it's undefined!... This is totally absurd. 如果我随后尝试获取errBody.message,则它是未定义的!...这完全荒谬。 What am I doing wrong? 我究竟做错了什么? How do you guys do this for a living? 你们如何以此为生? It's killing me! 这太痛苦了!

I'm assuming errorString is err._body ? 我假设errorStringerr._body Either way, parsing that string to JSON should be as simple as: 无论哪种方式,将该字符串解析为JSON都应该很简单:

let error = JSON.parse(err._body);

I came back to this recently. 我最近又回来了。 And finally managed to figure it out. 最终设法弄清楚了。 I needed to remove some unwanted backslashes in the body before trying JSON Parse. 在尝试JSON Parse之前,我需要删除体内的一些不需要的反斜杠。

const errorStringReplaced = err._body.replace(/\\/g, '');
const errBody = JSON.parse(errorStringReplaced);
this.outcomeMessage = errBody.message;

Having done that, I could then grab the innards properly. 完成此操作后,我就可以正确抓住内脏了。 I'd still prefer to send the object properly in the first place, but this will have to do for now. 我仍然希望首先正确地发送对象,但是现在必须这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON.parse 与 json_decode - JSON.parse vs json_decode JSON.parse(this.response) JavaScript 中的字符串问题 - JSON.parse(this.response) String issue in JavaScript Angular 9 HttpErrorResponse 'JSON.Parse error' 虽然响应正常 - Angular 9 HttpErrorResponse 'JSON.Parse error' while response is OK JSON.parse(hr.response)错误 - JSON.parse(hr.response) error SyntaxError:JSON.parse:第1行上的意外字符:Angular 5 Http调用(Angular JS工作正常) - SyntaxError: JSON.parse: unexpected character on line 1 : Angular 5 Http call(Angular JS was working fine) Angular 4 Restful Post响应,带有“ SyntaxError:JSON.parse处的JSON输入意外结束” - Angular 4 Restful Post response with “SyntaxError: Unexpected end of JSON input at JSON.parse” Angular HttpClient-如何从Http转换:JSON.parse(JSON.stringify(data))._ body)? - Angular HttpClient - how to convert from Http: JSON.parse(JSON.stringify(data))._body)? Ruby / Rails - JSON.parse / JSON.decode问题 - Ruby/Rails - issues with JSON.parse/JSON.decode Angular(8)Http.post()提供SyntaxError:JSON.parse位置0处JSON中的意外令牌O - Angular (8) Http.post() is giving SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse 意外的令牌:你的JSON.parse()问题 - Unexpected token: u JSON.parse() issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM