DELPHI XE10,JSON 生成和解析,再利用INDYHTTP控件POST

时间:2023-03-08 15:34:03
DELPHI XE10,JSON 生成和解析,再利用INDYHTTP控件POST
Delphi XE10,Json 生成和解析,再利用indyhttp控件Post
年09月20日 ::
阅读数:
--不多说,直接上代码
procedure TFrmMain.Brand;
var
JSONObject, jsonparam: TJSONObject; // JSON类
jsonArray: TJSONArray; // JSON数组变量
i: Integer;
Flag_Do: Boolean; jsonToSend: TStringStream;
jsonStr, RjsonStr: string;
RJSONObject, Rjsondata, RjsonlistObjItem: TJSONObject; // JSON类
RJsonArray: TJSONArray; // JSON数组变量
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 = then
begin
Flag_Do := False;
Break;
end;
First;
while not 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 := to RJsonArray.Size - 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
// 以列表的方式提交参数
IdHTTP.HandleRedirects := True; // 允许头转向
IdHTTP.ReadTimeout := ; // 请求超时设置
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded'; //'application/json'; // 设置内容类型为json Memo1.Clear;
Memo1.Text := AjsonStr; jsonToSend := TStringStream.Create(AjsonStr, TEncoding.UTF8);
jsonToSend.Position := ; // 将流位置置为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 := to len - 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 := to length(arrString) - do
begin
s1.Add(arrString[i]);
end; s1.Sort;
for i := to s1.Count - do
begin
arrString1[i] := s1.Strings[i];
end; TMPStr := TMPStr + app_secret; for i := to len - 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), '加密前', 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 := to len - 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 := to length(arrString) - do
begin
s1.Add(arrString[i]);
end; s1.Sort;
for i := to s1.Count - do
begin
arrString1[i] := s1.Strings[i];
end; TMPStr := ''; for i := to len - 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; --------------------------------------------------------------------------------------------------------------- 对于数值类型就需要使用TJSONNumber了,TJSONNumber支持Double、Integer、Int64等。使用方法如下 uses Data.DBXJSON; var
JSON: TJSONObject;
begin
JSON := TJSONObject.Create;
JSON.AddPair('Double', TJSONNumber.Create(123.456));
JSON.AddPair('Integer', TJSONNumber.Create());
JSON.AddPair('string', TJSONNumber.Create(''));
Memo1.Lines.Text := JSON.ToString; //=> {"Double":123.456,"Integer":6789,"string":1000}
JSON.Free; Boolean类型的值需要用到TJSONTrue和TJSONFalse。 uses System.JSON; var
JSON: TJSONObject;
begin
JSON := TJSONObject.Create;
JSON.AddPair('真', TJSONTrue.Create);
JSON.AddPair('假', TJSONFalse.Create);
Memo1.Lines.Text := JSON.ToString; //=> {"真":true,"假":false}
JSON.Free;