最近捡起丢了10年的Delphi来玩,想试试对json的数据处理。
试了TSuperObject和TlkJSONobject,都发现拉回的数据是乱码,运行exe就报错。
测试发现获取的json源数据中间汉字的地方是乱码,导致引号不配对,引发内存错误。
后来找了文章,发现是TIdHttp在获取数据之前,要将定义的TStringStream的Encoding设置为UTF8,才可以。
贴一小段源码,看官一看就懂的:
var
b: TStringStream; //用于接收json数据的流
s1: String;
begin
b := TStringStream.Create('',65001); //65001是UTF-8
IdHTTP1.Get('http://www.weather.com.cn/data/cityinfo/101010100.html', b); //获取中国气象局公开的json数据
s1 := b.DataString;
mmoJsonContent.Text := s1; //展示原始数据
另,关于编码的定义,在System.SysUtils单元有定义:
function GetCPInfo(CodePage: Cardinal; var lpCPInfo: TCPInfo): Boolean;
begin
Result := True;
case CodePage of
// Code page identifiers understood directly by iconv_open()
154, 367, 437, 737, 775, 819, 850, 852,
853, 855..858, 860..866, 869, 874, 922: lpCPInfo.MaxCharSize := 1;
932, 936, 943, 949, 950: lpCPInfo.MaxCharSize := 2;
1046, 1124, 1125, 1129, 1133, 1161, 1162, 1163, 1250..1258: lpCPInfo.MaxCharSize := 1;
1361: lpCPInfo.MaxCharSize := 2;
// Code page indentifiers translated to iconv_open() encoding names (by LocaleNameFromCodePage)
10000, 10004..10007, 10010, 10017, 10021,
10029, 10079, 10081, 10082: lpCPInfo.MaxCharSize := 1; // MacRoman .. MacCroatian
12000, 12001: lpCPInfo.MaxCharSize := 4; // UTF-32LE, UTF-32BE
20127, 20866: lpCPInfo.MaxCharSize := 1; // ASCII, KOI8-R
20932: lpCPInfo.MaxCharSize := 3; // EUC-JP
20936: lpCPInfo.MaxCharSize := 2; // GB2312, EUC-KR
21866, 28591..28601, 28603..28606: lpCPInfo.MaxCharSize := 1; // KOI8-U, ISO-8859-1..ISO-8859-16
50221: lpCPInfo.MaxCharSize := 9; // ISO-2022-JP
50225: lpCPInfo.MaxCharSize := 7; // ISO-2022-KR
50227: lpCPInfo.MaxCharSize := 8; // ISO-2022-CN
51932: lpCPInfo.MaxCharSize := 3; // EUC-JP
51936, 51949: lpCPInfo.MaxCharSize := 2; // GB2312, EUC-KR
51950, 52936, 54936: lpCPInfo.MaxCharSize := 4; // EUC-TW, HZ-GB-2312, GB18030
65000: lpCPInfo.MaxCharSize := 6; // UTF-7
65001: lpCPInfo.MaxCharSize := 4; // UTF-8
else
Result := False;
end;
end;