2

Following is my JSON:

{
  "forms": {
    "frmLogin": [
      {
        "frmLoginPg": "Se connecter - Application de gestion de PC"
      },
      {
        "lbl_login_Title": "Application de gestion Pc"
      },
      {
        "lbl_loginName": "Nom d'utilisateur"
      },
      {
        "lblLanguage": "langue préférée"
      },
      {
        "btnLogin": "Se connecter"
      },
      {
        "btnReset_Loginfrm": "Réinitialiser"
      }
    ],
    "frmHome": [
      {
        "frmHomepg": "Accueil"
      },
      {
        "lbladdUser_Title": "Ajouter un utilisateur"
      },
      {
        "lblName": "prénom"
      },
      {
        "lblEmail": "EMail"
      },
      {
        "popmemFile": "Fichier"
      }
    ]
  }
}

I am trying to get the values assigned to each key so I can change the Caption of each component.

I tried the following way, but I am getting an Invalid class typecast error:

function Translationspg.GetTranslationsJson(formNameJson, frmName_FORMJson
  : TComponentName; formsam: TForm): string;
var
  lJsonBytes: TBytes;
  lJsonVal, lJsonScenar: TJSONValue;
  lJsonScenarioValue: string; // lJsonString,
  lJsonObj: TJSONObject; // , lJsonScenario
  lJsonArray: TJSONArray;
  lJsonScenarioEntry: TJSOnString;
  lJsonPair: TJSONPair;
begin
  lJsonBytes := TFile.ReadAllBytes(scJSONFileName_French);
  lJsonScenar := TJSONObject.ParseJSONValue(lJsonBytes, 0);
  if lJsonScenar <> nil then
  begin
    lJsonArray := lJsonScenar as TJSONArray;

    for lJsonVal in lJsonArray do
    begin
      lJsonObj := lJsonVal as TJSONObject;
      lJsonPair := lJsonObj.Get(formNameJson);
      lJsonScenarioEntry := lJsonPair.JsonString;
      lJsonScenarioValue := lJsonScenarioEntry.Value;        
    end;
  end;
  Result := lJsonScenarioValue;
end;

1 Answer 1

7
lJsonArray := lJsonScenar as TJSONArray

The root of your JSON is not an array. It is an object. That objects has a single name/value pair, named forms. You need to read that, and then look for the form by name. Like this:

lJsonObj := TJSONObject.ParseJSONValue(lJsonBytes, 0) as TJSONObject;
lJsonObj := lJsonObj.GetValue('forms') as TJSONObject;
lJsonPair := lJsonObj.Get(formNameJson);
....

This program

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.JSON, System.IOUtils;

procedure Main(const fileName, formName: string);
var
  lJsonBytes: TBytes;
  lJsonObj: TJSONObject;
  lJsonArray: TJSONArray;
  lJsonValue: TJSONValue;
  lJsonPair: TJSONPair;
begin
  lJsonBytes := TFile.ReadAllBytes(fileName);
  lJsonObj := TJSONObject.ParseJSONValue(lJsonBytes, 0) as TJSONObject;
  lJsonObj := lJsonObj.GetValue('forms') as TJSONObject;
  lJsonArray := lJsonObj.GetValue(formName) as TJSONArray;
  Writeln(fileName, ' ', formName);
  for lJsonValue in lJsonArray do begin
    lJsonObj := lJsonValue as TJSONObject;
    for lJsonPair in lJsonObj do begin
      Writeln(lJsonPair.JsonString.ToString, ': ', lJsonPair.JsonValue.ToString);
    end;
  end;
  Writeln;
end;

begin
  try
    Main('C:\desktop\json.txt', 'frmLogin');
    Main('C:\desktop\json.txt', 'frmHome');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

has this output:

C:\desktop\json.txt frmLogin
"frmLoginPg": "Se connecter - Application de gestion de PC"
"lbl_login_Title": "Application de gestion Pc"
"lbl_loginName": "Nom d'utilisateur"
"lblLanguage": "langue préférée"
"btnLogin": "Se connecter"
"btnReset_Loginfrm": "Réinitialiser"

C:\desktop\json.txt frmHome
"frmHomepg": "Accueil"
"lbladdUser_Title": "Ajouter un utilisateur"
"lblName": "prénom"
"lblEmail": "EMail"
"popmemFile": "Fichier"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.