Forum > Networking and Web Programming

Unicode from JSON no win

(1/1)

Trenatos:
[Update]
Cyrax suggested I use SetMultiByteConversionCodePage(CP_UTF8); and it seems to have done the trick.
==============

I'm on OSX 10.13.6 High Sierra.

I'm making an http call to http://date.nager.at/api/v1/get/SE/2018 and it returns a JSON object.

It's an array of objects like this (But with more objects in the array)


--- Code: Pascal  window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---[  {    "counties" : null,    "countryCode" : "SE",    "countyAdministrationHoliday" : true,    "countyOfficialHoliday" : true,    "date" : "2018-01-01",    "fixed" : true,    "global" : true,    "launchYear" : null,    "localName" : "nyårsdagen",    "name" : "New Year's Day"  }] 
I'm trying to output the localName in the terminal but I just get ? for the å.

I've tried everything I can think of but I just can't get the output.

And yes, if I concatenate with something like WriteLn('öäå ' + holidayInfo.GetPath('localName').AsUnicodeString); those first three show up just fine.

Entire program as-is (Line 330 is where I'm outputting data):

--- Code: Pascal  window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program isTodayAHoliday; {$mode objfpc}{$H+} uses  sysutils, fphttpclient, fpjson, jsonparser; var  countries: Array [0..248] of String; procedure setUpCountryList;begin  // Set up the country array  countries[0] := 'AD';  countries[1] := 'AE';  countries[2] := 'AF';  countries[3] := 'AG';  countries[4] := 'AI';  countries[5] := 'AL';  countries[6] := 'AM';  countries[7] := 'AO';  countries[8] := 'AQ';  countries[9] := 'AR';  countries[10] := 'AS';  countries[11] := 'AT';  countries[12] := 'AU';  countries[13] := 'AW';  countries[14] := 'AX';  countries[15] := 'AZ';  countries[16] := 'BA';  countries[17] := 'BB';  countries[18] := 'BD';  countries[19] := 'BE';  countries[20] := 'BF';  countries[21] := 'BG';  countries[22] := 'BH';  countries[23] := 'BI';  countries[24] := 'BJ';  countries[25] := 'BL';  countries[26] := 'BM';  countries[27] := 'BN';  countries[28] := 'BO';  countries[29] := 'BQ';  countries[30] := 'BR';  countries[31] := 'BS';  countries[32] := 'BT';  countries[33] := 'BV';  countries[34] := 'BW';  countries[35] := 'BY';  countries[36] := 'BZ';  countries[37] := 'CA';  countries[38] := 'CC';  countries[39] := 'CD';  countries[40] := 'CF';  countries[41] := 'CG';  countries[42] := 'CH';  countries[43] := 'CI';  countries[44] := 'CK';  countries[45] := 'CL';  countries[46] := 'CM';  countries[47] := 'CN';  countries[48] := 'CO';  countries[49] := 'CR';  countries[50] := 'CU';  countries[51] := 'CV';  countries[52] := 'CW';  countries[53] := 'CX';  countries[54] := 'CY';  countries[55] := 'CZ';  countries[56] := 'DE';  countries[57] := 'DJ';  countries[58] := 'DK';  countries[59] := 'DM';  countries[60] := 'DO';  countries[61] := 'DZ';  countries[62] := 'EC';  countries[63] := 'EE';  countries[64] := 'EG';  countries[65] := 'EH';  countries[66] := 'ER';  countries[67] := 'ES';  countries[68] := 'ET';  countries[69] := 'FI';  countries[70] := 'FJ';  countries[71] := 'FK';  countries[72] := 'FM';  countries[73] := 'FO';  countries[74] := 'FR';  countries[75] := 'GA';  countries[76] := 'GB';  countries[77] := 'GD';  countries[78] := 'GE';  countries[79] := 'GF';  countries[80] := 'GG';  countries[81] := 'GH';  countries[82] := 'GI';  countries[83] := 'GL';  countries[84] := 'GM';  countries[85] := 'GN';  countries[86] := 'GP';  countries[87] := 'GQ';  countries[88] := 'GR';  countries[89] := 'GS';  countries[90] := 'GT';  countries[91] := 'GU';  countries[92] := 'GW';  countries[93] := 'GY';  countries[94] := 'HK';  countries[95] := 'HM';  countries[96] := 'HN';  countries[97] := 'HR';  countries[98] := 'HT';  countries[99] := 'HU';  countries[100] := 'ID';  countries[101] := 'IE';  countries[102] := 'IL';  countries[103] := 'IM';  countries[104] := 'IN';  countries[105] := 'IO';  countries[106] := 'IQ';  countries[107] := 'IR';  countries[108] := 'IS';  countries[109] := 'IT';  countries[110] := 'JE';  countries[111] := 'JM';  countries[112] := 'JO';  countries[113] := 'JP';  countries[114] := 'KE';  countries[115] := 'KG';  countries[116] := 'KH';  countries[117] := 'KI';  countries[118] := 'KM';  countries[119] := 'KN';  countries[120] := 'KP';  countries[121] := 'KR';  countries[122] := 'KW';  countries[123] := 'KY';  countries[124] := 'KZ';  countries[125] := 'LA';  countries[126] := 'LB';  countries[127] := 'LC';  countries[128] := 'LI';  countries[129] := 'LK';  countries[130] := 'LR';  countries[131] := 'LS';  countries[132] := 'LT';  countries[133] := 'LU';  countries[134] := 'LV';  countries[135] := 'LY';  countries[136] := 'MA';  countries[137] := 'MC';  countries[138] := 'MD';  countries[139] := 'ME';  countries[140] := 'MF';  countries[141] := 'MG';  countries[142] := 'MH';  countries[143] := 'MK';  countries[144] := 'ML';  countries[145] := 'MM';  countries[146] := 'MN';  countries[147] := 'MO';  countries[148] := 'MP';  countries[149] := 'MQ';  countries[150] := 'MR';  countries[151] := 'MS';  countries[152] := 'MT';  countries[153] := 'MU';  countries[154] := 'MV';  countries[155] := 'MW';  countries[156] := 'MX';  countries[157] := 'MY';  countries[158] := 'MZ';  countries[159] := 'NA';  countries[160] := 'NC';  countries[161] := 'NE';  countries[162] := 'NF';  countries[163] := 'NG';  countries[164] := 'NI';  countries[165] := 'NL';  countries[166] := 'NO';  countries[167] := 'NP';  countries[168] := 'NR';  countries[169] := 'NU';  countries[170] := 'NZ';  countries[171] := 'OM';  countries[172] := 'PA';  countries[173] := 'PE';  countries[174] := 'PF';  countries[175] := 'PG';  countries[176] := 'PH';  countries[177] := 'PK';  countries[178] := 'PL';  countries[179] := 'PM';  countries[180] := 'PN';  countries[181] := 'PR';  countries[182] := 'PS';  countries[183] := 'PT';  countries[184] := 'PW';  countries[185] := 'PY';  countries[186] := 'QA';  countries[187] := 'RE';  countries[188] := 'RO';  countries[189] := 'RS';  countries[190] := 'RU';  countries[191] := 'RW';  countries[192] := 'SA';  countries[193] := 'SB';  countries[194] := 'SC';  countries[195] := 'SD';  countries[196] := 'SE';  countries[197] := 'SG';  countries[198] := 'SH';  countries[199] := 'SI';  countries[200] := 'SJ';  countries[201] := 'SK';  countries[202] := 'SL';  countries[203] := 'SM';  countries[204] := 'SN';  countries[205] := 'SO';  countries[206] := 'SR';  countries[207] := 'SS';  countries[208] := 'ST';  countries[209] := 'SV';  countries[210] := 'SX';  countries[211] := 'SY';  countries[212] := 'SZ';  countries[213] := 'TC';  countries[214] := 'TD';  countries[215] := 'TF';  countries[216] := 'TG';  countries[217] := 'TH';  countries[218] := 'TJ';  countries[219] := 'TK';  countries[220] := 'TL';  countries[221] := 'TM';  countries[222] := 'TN';  countries[223] := 'TO';  countries[224] := 'TR';  countries[225] := 'TT';  countries[226] := 'TV';  countries[227] := 'TW';  countries[228] := 'TZ';  countries[229] := 'UA';  countries[230] := 'UG';  countries[231] := 'UM';  countries[232] := 'US';  countries[233] := 'UY';  countries[234] := 'UZ';  countries[235] := 'VA';  countries[236] := 'VC';  countries[237] := 'VE';  countries[238] := 'VG';  countries[239] := 'VI';  countries[240] := 'VN';  countries[241] := 'VU';  countries[242] := 'WF';  countries[243] := 'WS';  countries[244] := 'YE';  countries[245] := 'YT';  countries[246] := 'ZA';  countries[247] := 'ZM';  countries[248] := 'ZW';end; function isInCountryList(code: String):Boolean;var  i: Integer;begin  code := UpCase(code);  for i := 0 to 248 do begin    if countries[i] = code then begin      Result := true;      Exit;    end;  end;   Result := false;end; procedure iterateArrayItem(Item: TJSONData; Data: TObject; var Continue: Boolean);begin end; var  currentYear, currentYearMonthDay, objectDate: String;  objectHolidayName: unicodestring;  i: Integer;  jData: TJSONData;  holidayInfo: TJSONData;  jObject: TJSONObject;  jArray: TJSONArray;begin  if paramCount = 0 then begin    WriteLn('Usage: ./isTodayAHoliday <optional country code>, it will tell you.');    WriteLn('Country codes are ISO3166 Alpha-2 formatted. Sweden - SE, Barbados - BB, Japan - JP and so on.');    WriteLn('You can find a full list here https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2');    Halt;  end;   if length(paramStr(1)) <> 2 then begin    WriteLn('You can only use two-digit country codes');    Halt;  end;   // We have a two-character input, let's see if it's in the ISO list  setUpCountryList;   if isInCountryList(paramStr(1)) = false then begin    WriteLn('That''s not a valid Alpha-2 ISO country code');    Halt;  end;    // Finally, let's make the api call.  currentYear := FormatDateTime('YYYY', Now);  currentYearMonthDay := FormatDateTime('YYYY-MM-DD', Now);  try    // Get data    jData := GetJSON(TFPHTTPClient.SimpleGet('http://date.nager.at/api/v1/get/'+ paramStr(1) +'/' + currentYear));     jArray := TJSONArray(jData);     jObject := TJSONObject.Create;     for i := 0 to jArray.Count -1 do begin      jObject := TJSONObject(jArray.Items[i]);      holidayInfo := jArray.Items[i];       // No love :(      WriteLn(holidayInfo.GetPath('localName').AsUnicodeString);       objectDate := jObject.Get('date');      objectHolidayName := jObject.Get('localName');       if currentYearMonthDay = objectDate then begin        objectHolidayName := jObject.Get('localName');        WriteLn('Yes! Today is a holiday -- ', objectHolidayName);        Halt;      end;    end;    WriteLn('Today isn''t a holiday');   except    on E: Exception do      WriteLn(E.Message);  end;end.  

lucamar:
Sounds like a string encoding conflict. Try using UTF8 strings for everything, just to see if it solves anything.

Trenatos:
Switched every String variable to UTF8String, no change.

Cyrax:
You should set DefaultSystemCodePage (or use SetMultiByteConversionCodePage to set it) to CP_UTF8.

Trenatos:
Cyrax! If I ever meet you I owe you a case of beer.

I'm gonna dig into what it does and why it's needed, but it did the trick.

Navigation

[0] Message Index

Go to full version