2015-05-19 15 views
5

ho questo:Delphi XE7 DataSnap con puro JSON

function TWS.listJSON(const id: integer): TJSONObject; 
var LDataSets: TFDJSONDataSets; 
begin 
    LDataSets := the_list(id); //the_list:TFDJSONDataSets 
    try 
     Result := TJSONObject.Create; 
     TFDJSONInterceptor.DataSetsToJSONObject(LDataSets, Result); 
    finally 
     LDataSets.Free; 
    end; 
end; 

Tutto bene "la_lista()" avranno tutti i dati necessari dal mio selezionare e finalmente avrò il risultato. Alcuni client Java si collegheranno l'accesso a qualcosa di simile: http://localhost:8080/datasnap/rest/Tws/listJSON/123

Per fare una prova, ho installato un estensione Chrome chiamata Advanced Riposo client, e ottengo questo risultato:

{"list":"QURCUw4AAADGAQAA\/wABAAH\/Av8DBAAO...."} 

dopo fare alcuni cambiamenti nella TWebModule1.DSHTTPWebDispatcher1FormatResult();

Sembra un dato JSON compresso e, per quanto ne so, Java può gestirlo, ma non ne sono sicuro e preferirei un output JSON non compresso e puro. So che usare MORMOt può fare il trucco ma vorrei provare, dato che usare MORMOt dovrebbe imparare molto.

È possibile farlo, produrre JSON puro, utilizzando il server RAD Datasnap? Forse tutto è assolutamente corretto e io non lo so ...

+0

Ehi, hai avuto qualsiasi successo? Sto avendo lo stesso problema. –

+0

Stai ancora utilizzando XE7? Ho smesso con XE7 per quello e forse la versione più recente potrebbe fare il lavoro. – Magno

risposta

2

Non penso che funzionerà, presumo che tu abbia bisogno di unità Delphi per usare FireDacJsonreflect.

Si potrebbe rendere l'output JSON da soli, vedere questo piccolo esempio che uso comany dall'esempio DB solo il 3 campo in un ClientDataSet, si probobly renderlo un po 'più complicata, e anche un altro structur immagino

solo un'idea ..

function TServerMethods1.JsonDB: TJSONObject; // Hold the array 
var 
i  : Integer; 
JsonArray: TJSONArray; 
record_number : Integer; 

begin 
result:=TJSONObject.Create; 

// Field names 
JsonArray:=TJSONArray.Create; 
ClientDataSet1.First; 
for i := 0 to ClientDataSet1.Fields.Count-1 do 
Begin 
    JsonArray.AddElement(TJSONObject.Create(TJSONPair.Create('Field'+I.ToString ,ClientDataSet1.Fields[i].FieldName))); 
End; 
    Result.AddPair('Fields',JsonArray); 

//Data 
record_number:=0; 
while not ClientDataSet1.Eof do 
Begin 
    inc(record_number); 
    JsonArray:=TJSONArray.Create; 
    for i := 0 to ClientDataSet1.Fields.Count-1 do 
    Begin 
    JsonArray.AddElement(TJSONObject.Create(TJSONPair.Create(I.ToString,ClientD ataSet1.Fields[i].Asstring))); 
    End; 
    Result.AddPair('record-'+record_number.ToString,JsonArray); 
    ClientDataSet1.Next; 
End; 

end; 

Questo dovrebbe dare un risultato come

{ "risultato": [{ "Fields": [{ "Field0": "CUSTNO"}, { "Field1":" Azienda "}, {" Field2 ":" Paese "}]," record-1 ": [{" 0 ":" 1221 "}, {" 1 ":" Kauai Dive Shoppe "}, {" 2 ":" Stati Uniti "}]," reco RD-2 ": [{" 0 ":" 1231 "}, {" 1 ":" Unisco "}, {" 2 ":" Bahamas "}]," record-3 ": [{" 0 ":" 1351 "}, {" 1 ":" Sight Diver "}, {" 2 ":" Cyprus "}]," record-4 ": [{" 0 ":" 1354 "}, {" 1 ":" Cayman Divers World Unlimited "}, {" 2 ":" British West Indies "}]," record-5 ": [{" 0 ":" 1356 "}, {" 1 ":" Tom Sawyer Diving Center "}, { "2": "Isole Vergini americane"}], "record-6": [{"0": "1380"}, {"1": "Blue Jack Aqua Center"}, {"2": "Stati Uniti" }], "record-7": [{"0": "1384"}, {"1": "VIP Divers Club"}, {"2": "Isole Vergini americane"}], "record-8" : [{"0": "1510"}, {"1": "Ocean Paradise"}, {"2": "US"}], "record-9": [{"0": "1513"} , {"1": "Fantastique Aquatica"}, {"2": "Columbia"}], "record-10": [{"0": "1551"}, {"1": "Marmot Divers Club" }, {"2": "Canada"}], "record-11": [{"0": "1560"}, {"1": "Depressione"}, {"2": "Stati Uniti" }], "record-12": [{"0": "1563"}, {"1": "Blue Sports"}, {"2": "US"}], "record-13": [{ "0": "1624"}, {"1": "Makai SCUBA Club"}, {"2": "US"}], "record-14": [{"0": "1645"}, { "1": "Action Club"}, {"2": "US"}], "record-15": [{"0": "1651"}, {"1": "Jamaica SCUBA Center"}, {"2": "West Indies"}], "record-16": [{"0": "1680"}, {"1": "Ricerca isola"} , { "2": "USA"}], "record-17": [{ "0": "1984"}, { "1": "Avventura Undersea"}

+0

Grazie. Darò una prova in base al tuo suggerimento e riferirò qui. – Magno

+0

Ciao, un altro modo è quello di creare una classe e riempirla con i dati ed esportare la classe in Json dare un'occhiata a questo link. http://firemonkeytutorial.com/using-model-view-viewmodel-mvvm-delphi-firemonkey/ e questo http://blogs.embarcadero.com/stephenball/2014/06/05/how-to-convert-an- oggetto a json-e-back-con-una-singola linea di codice / – bsw

0
procedure TWebModuleServer.DSHTTPWebDispatcher1FormatResult(Sender: TObject; 
    var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean); 
var 
JSONValue: TJSONValue; 
begin 
    if Command.Text = 'TServerMethodsServer.GetVendas' then 
    begin 
     Handled := True; 
     JSONValue := ResultVal; 
     ResultVal := TJSONArray(JSONValue).Get(0); 
     TJSONArray(JSONValue).Remove(0); 
     JSONValue.Free; 
    end; 
end; 
Problemi correlati