Delphi XE10,Json 生成和解析,再利用indyhttp控件Post2017年09月20日 18:30:23阅读数:1973--不多说,直接上代码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 = 0 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 := 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,100,SbrandID); Parameters.CreateParameter(‘vbrandCode‘,ftString,SbrandCode); Parameters.CreateParameter(‘vchinesename‘,Schinesename); Parameters.CreateParameter(‘venglishname‘,Senglishname); Parameters.CreateParameter(‘vlogoUrl‘,SlogoUrl); Parameters.CreateParameter(‘vintroduction‘,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 := 5000; // 请求超时设置 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; // 将流位置置为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),‘加密前‘,mrOk); TMPMD5 := UpperCase(md5.GetHashString(TMPStr)); Result := TMPMD5;end;function getFormData(params: TJsONObject): string;var arrString,arrString1: array of string; // ItemArry:TJsONArray; len,formData,TMP1: string; md5: THashMD5;begin len := params.Count; SetLength(arrString,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;---------------------------------------------------------------------------------------------------------------对于数值类型就需要使用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(6789)); JsON.AddPair(‘string‘,TJsONNumber.Create(‘1000‘)); 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;个人分类:总结
以上是内存溢出为你收集整理的Delphi XE10,Json 生成和解析,再利用indyhttp控件Post全部内容,希望文章能够帮你解决Delphi XE10,Json 生成和解析,再利用indyhttp控件Post所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)