Einzelnen Beitrag anzeigen

fisipjm

Registriert seit: 28. Okt 2013
299 Beiträge
 
#3

AW: JsonToObject überreden ein leeres Objekt zu verarbeiten

  Alt 6. Feb 2024, 08:38
Moin Uwe,

hier mal ein Stück Code. Das Müsste es eigentlich recht gut verdeutlichen:

Delphi-Quellcode:
uses
  ...REST.JSON...



type
  TMyJSONObject = class
  private
    FValue1: String;
    FValue2: String;
  public
    property Value1: String read FValue1 write FValue1;
    property Value2: String read FValue2 write FValue2;
  end;

type
  TMyJsonNestedObject = class
  private
    FNeuesLeeresObject: TMyJSONObject;
  published
    property NeuesLeeresObject: TMyJSONObject read FNeuesLeeresObject write FNeuesLeeresObject;
  end;

procedure Foo;
var
  MyJSONObject : TMyJSONObject;
  MyJsonNestedObject: TMyJsonNestedObject;
begin
  MyJSONObject := TMyJSONObject.create;
  try
    MyJsonNestedObject := TMyJsonNestedObject.Create;
    try
      MyJSONObject := TJson.JsonToObject<TMyJSONObject>('{'+
                                                            '"Value1":"Ich bin Wert1",'+
                                                            '"Value2":"Ich bin Wert2"'+
                                                        '}');
      ShowMessage('Geht');

      MyJSONObject := TJson.JsonToObject<TMyJSONObject>('{'+
                                                            '"Value1":"Ich bin Wert1",'+
                                                            '"Value2":"Ich bin Wert2",'+
                                                            '"NeuerWert":"-NeuerWert-"'+
                                                        '}');
      ShowMessage('Geht immer noch');

      MyJSONObject := TJson.JsonToObject<TMyJSONObject>('{'+
                                                            '"Value1":"Ich bin Wert1",'+
                                                            '"Value2":"Ich bin Wert2",'+
                                                            '"NeuerWert":"-NeuerWert-",'+
                                                            '"NeuesLeeresArray":["ich","hab","sogar","Werte"]'+
                                                        '}');
      ShowMessage('Geht weiterhin');

      MyJSONObject := TJson.JsonToObject<TMyJSONObject>('{'+
                                                            '"Value1":"Ich bin Wert1",'+
                                                            '"Value2":"Ich bin Wert2",'+
                                                            '"NeuerWert":"-NeuerWert-",'+
                                                            '"NeuesLeeresArray":["ich","hab","sogar","Werte"],'+
                                                            '"NeuesLeeresObject":[{}]'+
                                                        '}');
      ShowMessage('Geht sogar im Array');

      try
        MyJSONObject := TJson.JsonToObject<TMyJSONObject>('{'+
                                                              '"Value1":"Ich bin Wert1",'+
                                                              '"Value2":"Ich bin Wert2",'+
                                                              '"NeuerWert":"-NeuerWert-",'+
                                                              '"NeuesLeeresArray":["ich","hab","sogar","Werte"],'+
                                                              '"NeuesLeeresObject":{}'+
                                                          '}');
      except
        on e:exception do
        begin
          showmessage(e.ClassName + ' - ' + e.Message);
        end;
      end;

      MyJsonNestedObject := TJson.JsonToObject<TMyJsonNestedObject>('{'+
                                                              '"Value1":"Ich bin Wert1",'+
                                                              '"Value2":"Ich bin Wert2",'+
                                                              '"NeuerWert":"-NeuerWert-",'+
                                                              '"NeuesLeeresArray":["ich","hab","sogar","Werte"],'+
                                                              '"NeuesLeeresObject":{}'+
                                                          '}');
      showmessage('Geht wieder');

      MyJsonNestedObject := TJson.JsonToObject<TMyJsonNestedObject>('{'+
                                                              '"Value1":"Ich bin Wert1",'+
                                                              '"Value2":"Ich bin Wert2",'+
                                                              '"NeuerWert":"-NeuerWert-",'+
                                                              '"NeuesLeeresArray":["ich","hab","sogar","Werte"],'+
                                                              '"NeuesLeeresObject":{"Value1":"Nested Value1"}'+
                                                          '}');
      showmessage('Sogar mit Wert: '+MyJsonNestedObject.NeuesLeeresObject.FValue1);
    finally
      MyJsonNestedObject.Free;
    end;
  finally
    MyJSONObject.Free;
  end;
end;
  Mit Zitat antworten Zitat