Delphi Xe10,json Generation and parsing, then utilizes indyhttp control POST__JS

Source: Internet
Author: User
Tags md5 stringreplace
--not much, directly on the code
Procedure Tfrmmain.brand;
Var
Jsonobject, Jsonparam:tjsonobject; JSON class
Jsonarray:tjsonarray; JSON array variable
I:integer;
Flag_do:boolean;

Jsontosend:tstringstream;
Jsonstr, rjsonstr:string;
Rjsonobject, Rjsondata, Rjsonlistobjitem:tjsonobject; JSON class
Rjsonarray:tjsonarray; JSON array variable
Infocount:integer;
Tmpstr, tmpitemvalue:string;
sbrandid:double;
Sbrandcode, Schinesename, Senglishname, Slogourl, sintroduction:string;
Infocode, infomsg:string;
Begin
FLAG_DO: = True;
URL: = ' http://devhhplus.oudianyun.com:2035/open-api/product-web/brand/getBrand.do ';

Try
While Flag_do do
Begin
Jsonobject: = tjsonobject.create;
Jsonparam: = tjsonobject.create;
Jsonarray: = tjsonarray.create;

With Dmmain.adoquery1 do
Begin
Close;
Parameters.clear;
Sql. Clear;
Sql. Add (' {call Pkg_synzhongtai.sp_getbrand (?)} ');
Sql. Add (' {call Pkg_synzhongtai.sp_getbrand} ');
Open;
If RecordCount = 0 Then
Begin
FLAG_DO: = False;
break;
End
A;
While does Eof do
Begin
Jsonarray.add (Fieldbyname (' Brandid '). asstring);
Jsonobject.addpair (' Brandid ', jsonarray);
Jsonobject.addpair (' brandname ', fieldbyname (' brandname '). asstring);
Next;
End
End

Jsonparam. Addpair (' app_id ', app_id);
Jsonparam. Addpair (' V ', ' 1.0 ');
Jsonparam. Addpair (' Jsondata ', jsonobject);
Jsonparam. Addpair (' sign ', getsign (Jsonparam));

JSONSTR: = Getformdata (Jsonparam);

RJSONSTR: = Sendpost (URL, JSONSTR);

Rjsonobject: = Tjsonobject.parsejsonvalue (RJSONSTR) as Tjsonobject;
Rjsondata: = Tjsonobject.parsejsonvalue (Trim (rjsonobject.getvalue (' data '). ToString)) as Tjsonobject;
Infocount: = Strtoint (Rjsondata.getvalue (' total '). ToString);

Infocode: = Rjsonobject.getvalue (' code '). ToString;
Rjsonobject.trygetvalue (' msg ', infomsg);
Rjsonarray: = Tjsonarray (Rjsondata.getvalue (' listobj '));

For I: = 0 to Rjsonarray.size-1 do
Begin
Rjsonlistobjitem: = Tjsonobject.parsejsonvalue (rjsonarray.items[i). ToString) as Tjsonobject;
Memo2.Lines.Add (' Brandid: ' + rjsonlistobjitem.getvalue (' Brandid '). ToString);
If Rjsonlistobjitem.trygetvalue (' Brandid ', tmpitemvalue) then
Sbrandid: = Strtofloat (Tmpitemvalue);
If Rjsonlistobjitem.trygetvalue (' Brandcode ', tmpitemvalue) then
Sbrandcode: = Tmpitemvalue;
If Rjsonlistobjitem.trygetvalue (' Chinesename ', tmpitemvalue) then
Schinesename: = Tmpitemvalue;
If Rjsonlistobjitem.trygetvalue (' Englishname ', tmpitemvalue) then
Senglishname: = Tmpitemvalue;
If Rjsonlistobjitem.trygetvalue (' Logourl ', tmpitemvalue) then
Slogourl: = Tmpitemvalue;
If Rjsonlistobjitem.trygetvalue (' Introduction ', Tmpitemvalue) then
Sintroduction: = Tmpitemvalue;

With Dmmain.adostoredproc1 do
Begin
Close;
Parameters.clear;
ProcedureName: = ' pkg_synzhongtai.sp_synztbrandweb ';
Parameters.createparameter (' Vbrandidweb ', Ftfloat, Pdinput, Sbrandid);
Parameters.createparameter (' Vbrandcode ', ftstring, Pdinput, Sbrandcode);
Parameters.createparameter (' Vchinesename ', ftstring, Pdinput, schinesename);
Parameters.createparameter (' Venglishname ', ftstring, Pdinput, senglishname);
Parameters.createparameter (' Vlogourl ', ftstring, Pdinput, Slogourl);
Parameters.createparameter (' vintroduction ', ftstring, Pdinput, sintroduction);
Execproc;
End
End
Logwrite (' Code: ' + Infocode + '. Msg: ' + infomsg);

Jsonobject: = nil;
Jsonparam: = nil;
Jsonarray: = nil;
Rjsonobject: = nil;
Rjsondata: = nil;
Rjsonlistobjitem: = nil;
Rjsonarray: = nil;
End
Finally

End

End


function Tfrmmain.sendpost (Aurl, ajsonstr:string): string;
Var
Jsontosend:tstringstream;
tmpresult:string;
Begin
Try
Try
Submit parameters as a list
Idhttp.handleredirects: = True; Allow head steering
Idhttp.readtimeout: = 5000; Request Timeout setting
IdHTTP.Request.ContentType: = ' application/x-www-form-urlencoded '; ' Application/json '; Set content type to JSON

Memo1.clear;
Memo1.text: = Ajsonstr;

Jsontosend: = Tstringstream.create (Ajsonstr, Tencoding.utf8);
Jsontosend.position: = 0; Place the flow position to 0

Idhttp.post (Url,jsontosend,responsestream);
Tmpresult: = Idhttp.post (Aurl, jsontosend);
Except
On E:exception do
Begin
ShowMessage (E.message);
End
End
Finally
Logwrite (Tmpresult);
Result: = Tmpresult;
End
End
function Getsign (params:tjsonobject): string;
Var
Arrstring, Arrstring1:array of string;
Len, I:integer;
S1:tstringlist;
Tmpstr, Tmpvalue, TMP1, tmpmd5:string;
MD5:THASHMD5;
Begin
Params. Removepair (' sign ');
Len: = params. Count;
SetLength (arrstring, Len);
SetLength (ArrString1, Len);

For I: = 0 to Len-1 do
Begin
Arrstring[i]: = params. Get (i). ToString;
Arrstring[i]: = params. Pairs[i]. jsonstring.tostring;
Arrstring[i]: = StringReplace (params. Pairs[i]. Jsonstring.tostring, ' "', ', [Rfreplaceall]);
ShowMessage (Arrstring[i]);
End
itemarry:= params. GetValue (' Jsondata ') as Tjsonarray;

S1: = Tstringlist.create;
For I: = 0 to Length (arrstring)-1 do
Begin
S1. ADD (Arrstring[i]);
End

S1. Sort;
For I: = 0 to S1. Count-1 do
Begin
Arrstring1[i]: = S1. Strings[i];
End

TMPSTR: = Tmpstr + App_secret;

For I: = 0 to Len-1 do
Begin
Tmpvalue: = params. GetValue (Arrstring1[i]). ToString;
ShowMessage (Arrstring1[i]);
ShowMessage (StringReplace (arrstring1[i], ' "', ', ', '", [Rfreplaceall]));
TMP1: = StringReplace (Arrstring1[i], ' ",", [Rfreplaceall]);
ShowMessage (TMP1);
ShowMessage (params. ToString);
ShowMessage (params. values[' Jsondata ']. ToString);
Tmpvalue: = params. VALUES[TMP1]. ToString;
If arrstring1[i] <> ' jsondata ' then
Begin
Tmpvalue: = StringReplace (Tmpvalue, ' "', ', ', [Rfreplaceall]);
End

If Tmpvalue <> ' then
Begin
TMPSTR: = Tmpstr + arrstring1[i] + tmpvalue;
End
End
TMPSTR: = Tmpstr + App_secret;
Application.messagebox (Pchar (TMPSTR), ' Encrypt before ', Mrok);

TMPMD5: = Uppercase (MD5. Gethashstring (TMPSTR));
Result: = TMPMD5;
End

function Getformdata (params:tjsonobject): string;
Var
Arrstring, Arrstring1:array of string;
Itemarry:tjsonarray;
Len, I:integer;
S1:tstringlist;
Tmpstr, Tmpvalue, FormData, tmp1:string;

MD5:THASHMD5;
Begin
Len: = params. Count;
SetLength (arrstring, Len);
SetLength (ArrString1, Len);

For I: = 0 to Len-1 do
Begin
Arrstring[i]: = params. Get (i). ToString;
Arrstring[i]: = params. Pairs[i]. jsonstring.tostring;
Arrstring[i]: = StringReplace (params. Pairs[i]. Jsonstring.tostring, ' "', ', [Rfreplaceall]);
End
itemarry:= params. GetValue (' Jsondata ') as Tjsonarray;

S1: = Tstringlist.create;
For I: = 0 to Length (arrstring)-1 do
Begin
S1. ADD (Arrstring[i]);
End

S1. Sort;
For I: = 0 to S1. Count-1 do
Begin
Arrstring1[i]: = S1. Strings[i];
End

TMPSTR: = ';

For I: = 0 to Len-1 do
Begin
Tmpvalue: = params. GetValue (Arrstring1[i]). ToString;
TMP1: = StringReplace (Arrstring1[i], ' ",", [Rfreplaceall]);
If arrstring1[i] <> ' jsondata ' then
Begin
Tmpvalue: = StringReplace (params. VALUES[TMP1]. ToString, ' "', ', ', [Rfreplaceall]);//params. VALUES[TMP1]. ToString;
End
Else
Begin
Tmpvalue: = params. VALUES[TMP1]. ToString;
End

If Tmpvalue <> ' then
Begin
If Tmpstr <> ' then
TMPSTR: = tmpstr + ' & ';

TMPSTR: = Tmpstr + arrstring1[i] + ' = ' + tmpvalue;
End
End
Result: = TMPSTR;
End


------------------------------------------------------------------------------------------------------------ ---need to use tjsonnumber for numeric types, Tjsonnumber supports double, Integer, Int64, and so on. Use the method below

uses Data.dbxjson;

var
  Json:tjsonobject;
Begin
  JSON: = Tjsonobject.create;
  JSON. Addpair (' Double ', Tjsonnumber.create (123.456));
  JSON. Addpair (' Integer ', Tjsonnumber.create (6789));
  JSON. Addpair (' String ', Tjsonnumber.create (' 1000 '));
  Memo1.Lines.Text: = JSON. tostring; //=> {"Double": 123.456, "Integer": 6789, "string": 1000}
  JSON. Free; The value of the

Boolean type requires the use of tjsontrue and Tjsonfalse.

uses System.json;

var
  Json:tjsonobject;
Begin
  JSON: = Tjsonobject.create;
  JSON. Addpair (' true ', tjsontrue.create);
  JSON. Addpair (' false ', tjsonfalse.create);
  Memo1.Lines.Text: = JSON. tostring; //=> {true: True, False: false}

Json. Free;




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.