0

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 }

jayAnn
  • 827
  • 3
  • 18
  • 38
  • There is no place in the code where the 'Hello ' appears. And `ResultValue := TlkJSONlist.Create;` is executed N times but then gets overwritten. – mjn Jun 23 '11 at 13:23
  • yes, thank you. I already have the code. Let me share it to all. :) – jayAnn Jun 24 '11 at 02:44

2 Answers2

1

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.

jayAnn
  • 827
  • 3
  • 18
  • 38
0

First of all, I think your URI shown in your question is already decoded. You should encode the URI parameters in the HTTP protocol.

If you want to create such HTTP-oriented JSON access, take a look at the RESTful approach. It would help you not reinvent the well, and be able to make your server more AJAX ready.

Then you seems to use the third-party lkJSON Delphi library... So you could get directly help from its author or support forum.

From the source code of the library, you should use a TlkJSONlist instance to handle a JSON array, from both URI input and result output.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • yes thank you. I just knew about TlkJSONlist. But i don't know how to use it... Sample code would be greatly appreciated. – jayAnn Jun 21 '11 at 06:03
  • Perhaps go and use another better documented and supported library, like http://www.progdigy.com/?page_id=6 – Arnaud Bouchez Jun 21 '11 at 12:40
  • ok, thanks. But I just can't used any other library. :) Maybe I just have to figure it out. I'm really stuck up. :) – jayAnn Jun 22 '11 at 02:58