userhi userhi - 9 days ago 6
JSON Question

Delphi parsing a Json with multiple array types?

Following is my json, Iam trying to get the values assigned to each key and i want to change the caption of the Component , Assigning of captions is done in xml

{
"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 tried in this way but i am getting a 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;


can someone guide me in proper way?

Answer
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"