Einzelnen Beitrag anzeigen

Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.388 Beiträge
 
Delphi 12 Athens
 
#6

AW: Delphi und JSON

  Alt 6. Mai 2021, 14:15
Hallöle...

JSON zu Object:
Delphi-Quellcode:
class function TToolsJson.ObjectFromJson(aJson: string): TObject;
var
  UnMarshalObj: TJSONUnMarshal;
  JSONObject: TJSONObject;
begin
  Result := nil;
  UnMarshalObj := TJSONUnMarshal.Create;
  try
    JSONObject := TJSONObject.ParseJSONValue(aJson) as TJSONObject;
    try
      if Assigned(JSONObject) then
        Result := UnMarshalObj.Unmarshal(JSONObject);
    finally
      JSONObject.Free;
    end;
  finally
    UnMarshalObj.Free;
  end;
end;


class function TToolsJson.JsonFromObject(aValue: TObject): string;
var
  MarshalObj: TJSONMarshal;
  JSONObject: TJSONObject;
begin
  Result := '';
  MarshalObj := TJSONMarshal.Create;
  try
    JSONObject := MarshalObj.Marshal(aValue) as TJSONObject;
    try
      if Assigned(JSONObject) then
        Result := JSONObject.ToString;
    finally
      JSONObject.Free;
    end;
  finally
    MarshalObj.Free;
  end;
end;
  Mit Zitat antworten Zitat