I use Rad studio 11. I read information from json file (file is UTF8 encoded) and convert to jsonobject. Then I make changes to this jsonobject and want to save to json file. The information is successfully written to the file, but the file has the Windows-1251 encoding. What needs to be done to make the file encoding UTF8? It's need me because json file include russian symbols (in Windows-1251 encoding it's looking like '?').
I read from a file like this:
var inputfile:TextFile;
str:string;
...
if OpenDialog1.Execute then begin
AssignFile(inputfile, OpenDialog1.FileName);
reset(inputfile);
while not Eof(inputfile) do
begin
ReadLn(inputfile, str);
str1 := str1+UTF8ToANSI(str);
end;
closefile(inputfile);
end;
I convert to Jsonobject like this:
LJsonObj:=TJSONObject.ParseJSONValue(str1) as TJSONobject;
Trying to save JsonObject like this:
var
listStr: TStringList;
Size: Integer;
I: Integer;
...
Size := Form3.LJsonObj.Count;
liststr := TStringList.Create;
try
listStr.Add('{');
if Size > 0 then
listStr.Add(LJsonObj.Get(0).ToString);
showmessage(LJsonObj.Get(0).ToString);
for I := 1 to Size - 1 do
begin
listStr.Add(',');
listStr.Add(ANSITOUTF8(LJsonObj.Get(I).ToString));
end;
listStr.Add('}');
// Form1.filepath-is path of file,form1.filename-name of file without file extension
listStr.SaveToFile(Form1.filepath+'\'+form1.filename+'.json');
finally
listStr.Free;
end;