Delphi 7 - Charset REST JSON

Fus*_*ube 2 delphi rest json indy character-encoding

使用Delphi 7和Indy 9.00.10我正在使用带有JSON的REST API.我像这样用TidHTTP组件创建一个GET请求.

IdHTTP1.HandleRedirects := True;
IdHTTP1.ReadTimeout     := 5000;
IdHTTP1.Request.Accept  := 'application/json';
IdHTTP1.Request.AcceptCharSet  := 'UTF-8';
IdHTTP1.Request.AcceptLanguage := 'sv';
IdHTTP1.Request.ContentType    := 'application/json';
Memo1.Text := IdHTTP1.Get('http://api.arbetsformedlingen.se/af/v0/platsannonser/7088149');
Run Code Online (Sandbox Code Playgroud)

我尝试了几个字符集,但无法在响应中更正å,ä,ö等瑞典字符.

  • å变成Ã¥
  • ä成为Ã
  • ö成为Ã

我在这做错了什么?

Tom*_*erg 7

在Delphi 7中,System有一个UTF8ToAnsi()功能.使用这样:

  Memo1.Text := UTF8ToAnsi(IdHTTP1.Get('http://api.arbetsformedlingen.se/af/v0/platsannonser/7088149'));
Run Code Online (Sandbox Code Playgroud)

结果在Delphi 7中是正确的.

  • 在Delphi 7中,您可以使用`UTF8Decode()`将UTF-8编码的`AnsiString`解码为Unicode`WideString`.但在这种情况下,最好使用`Tdream`版本的`TIdHTTP.Get()`,这样你就可以将原始XML数据按原样传递给你选择的XML解析器.不要将XML解析为Ansi字符串.让XML引擎使用XML的实际字符集为您解析它. (2认同)