-1

I'm using SuperObject library for working with JSON.

This code creates JSON:

procedure TfmMain.btnIngredientsSaveClick(Sender: TObject);
var obj: ISuperObject;
    i: integer;
begin
try
  obj := SO();
  for i := 0 to sgIngredients.RowCount - 2 do
    begin
      obj.O[sgIngredients.Cells[0, i+1]] := SA([]);
      obj.A[sgIngredients.Cells[0, i+1]].S[0] := sgIngredients.Cells[1, i+1];
      obj.A[sgIngredients.Cells[0, i+1]].S[1] := sgIngredients.Cells[2, i+1];
    end;
  obj.SaveTo(ExtractFileDir(Application.ExeName)+ingrJSONFile);
finally
  obj := nil;
end;
end;

sgIngredients - TStringGrid

sgIngredients contain cyrillic symbols. So output file is:

{
"4":["Hello","count"],
"3":["\u0411\u0443\u043b\u044c\u043e\u043d \u043e\u0432\u043e\u0449\u043d\u043e\u0439","\u0441\u0442."],
"2":["\u0411\u0443\u043b\u044c\u043e\u043d \u043a\u0443\u0440\u0438\u043d\u044b\u0439","\u0441\u0442."],
"1":["\u0411\u0435\u043a\u043e\u043d","\u0433\u0440."]
}

How to correctly save my data to JSON-file?

EDIT

This is screenshot of my string grid.

enter image description here

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Romowski
  • 1,518
  • 5
  • 25
  • 50
  • What problem are you having? Is the output file producing undesired results? What results are you expecting? What data do you have in this string grid? – Jerry Dodge May 29 '13 at 02:51
  • What makes you think it isn't correct already? It looks OK to me. – Rob Kennedy May 29 '13 at 04:16
  • @RobKennedy: it's OK but I would like to save normal text like in string grid – Romowski May 29 '13 at 04:53
  • I guess what your point is, is that JSON supports UTF-8 chars, so you should be able to save the actual cyrillic characters in the file, instead of the 'safer' conversion codes? – Anders E. Andersen May 29 '13 at 05:42

1 Answers1

3

Reading the sources, you can call function TSuperObject.SaveTo(stream: TStream; indent, escape: boolean): integer; setting escape := false

I can say it again, when using libraries with their source code given, just "Use the Source, Luke"

Also, u may save JSON to string, and then replace escaped characters with actual WideChar values (like was done in http://UniRed.sf.net or at http://www.sql.ru/forum/936760/perevesti-kodirovannye-simvoly-funkciya-v-delphi-analog-iz-js) and then save the resulting string to file while enforcing UTF-8 charset.

Arioch 'The
  • 15,799
  • 35
  • 62