Search code examples
jsondelphiarraylistdelphi-2005

Delphi webservice JSON array


I am really new to Delphi and I am doing an experiment on how to output JSON array through delphi. This maybe sound simple to anyone but I just dont know how. I already created a simple program.

Now, what i want to do is to create a command/request with parameter like:

http://localhost:8001/hello?json={"names":["Jay","Chris","John"]}

that would create a result in the browser like this:

{ result: ["Hello Jay","Hello Chris","Hello John"], id: "", time_elapsed: 0 }

Please, i really need help on this. Anybody?

EDIT: This is the code i just did today but it still doesn't show my desired output:

procedure TPrimeJSONMHelloPeople.ProcessJSONRPCRequest(
  var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
  var

    jsonPeople:TlkJSONlist;
    dmPool:TObject;
    dm:TPrimeDataModuleBaseDM;
    i:integer;

  begin
    FjsonObj1 := TlkJSONobject.Create;
    jsonPeople := FjsonObj1.CreateListValue('names');
    jsonPeople.AddVarString('jay');
    jsonPeople.AddVarString('ann');
    jsonPeople.AddVarString('john');
    inherited;

    CheckRequiredParameter('names');

    PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
      try
         dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
         try

         //this part here will loop and output the name
         //if jsonPeople <> nil then

         if Params.Field['names'] <> nil then
           begin
             for i := 0 to FjsonObj1.Field['names'].Count - 1 do
             begin           
               ResultValue := TlkJSONlist.Create
             end;

         end;
       ResultValue := TlkJSONlist.Create;
       finally
       dm.Release;
      end;
    finally
  dmPool.Free;
 end;
   FjsonObj1.Free;
   ResultSuccess := True;
 end;

I don't know what's missing in the code, It only shows: {

result: [ ],
id: "",
time_elapsed: 0

}

and not : { result: ["Hello Jay","Hello Chris","Hello John"], id: "", time_elapsed: 0 }


Solution

  • i have just found the right answer. Here's the code:

    procedure TSample1.ProcessJSONRPCRequest(
      var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
      var
    
        dmPool:TObject;
        dm:TPrimeDataModuleBaseDM;
    
        jsonPeople:TlkJSONlist;    //used Tlkjsonlist since I want to create an array
        i:integer;
      begin
      inherited;
        jsonPeople:=TlkJSONlist.Create;  //create jsonPeople as an array
    
        CheckRequiredParameter('names'); //names parameter needed
        PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
        try
          dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
          try
            if Params.Field['names'] <> nil then //check if the names parameter is empty
            begin
              ResultValue:=jsonPeople;
              for i := 0 to Params.Field['names'].Count - 1 do
              begin
                jsonPeople.AddVarString('hello ' + Params.Field['names'].Child[i].value);
              end;
            end;
          finally
            dm.Release;
          end;
        finally
        dmPool.Free;
      end;
      ResultSuccess := True;
    end;
    
    end.
    

    The request is http://localhost/sample1?json={"names":["john","jay"]} The output is

    {
    
        -
        result: [
            "hello john"
            "hello jay"
        ]
        id: ""
        time_elapsed: 0
    
    }
    

    Hope this can help someone who is new in creating web service request using delphi.