Iam having a stringGrid, The fields in First row are from CSV and in Second row are ColumnNames from a table in DB.
Now after click on save btn it should create me a Json file like below
{
"GHARGA": "GHARGA",
"SELLINGPRICE1": "SELLPRICE1",
"SELLPRICE2": "SELLPRICE2",
"SELLPRICE3": "SELLPRICE3",
"PCODE": "PCODE"
}
I tried this like this:
procedure TfrmMapping.btnsaveClick(Sender: TObject);
var
Dat1: TStringList;
i: Integer;
lJsonObj: TJSONObject;
begin
lJsonObj := TJSONObject.Create;
Dat1 := TStringList.Create;
if btnsave.Caption = 'Save' then
begin
for i := 0 to StrGrdCsv.RowCount - 1 do
begin
Dat1.AddStrings(StrGrdCsv.Rows[i]);
end;
for i := Dat1.Count - 1 Downto StrGrdCsv.ColCount do
begin
lJsonObj.AddPair(TJSONPair.Create(TJSONString.Create(Dat1.Strings[i]),
TJSONString.Create(Dat1.Strings[i])));
end;
TFile.WriteAllText(cmbBXDBTables.Text + '.json', lJsonObj.ToString);
btnsave.Caption := 'Load Data';
end;
end;
but it is creating like
{
"GHARGA": "GHARGA",
"SELLPRICE1": "SELLPRICE1",
"SELLPRICE2": "SELLPRICE2",
"SELLPRICE3": "SELLPRICE3",
"PCODE": "PCODE"
}
Modifed the Code as per suggestions by @DavidHeffemann and @RemyLeebeau
Modified the For loop and made it work using Cells of String Grid.
Thank You, @DavidHeffemann and @RemyLeebeau for your suggestions.