码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi XE10,Json 生成和解析,再利用indyhttp控件Post

时间:2018-06-08 10:33:40      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:text   cat   remove   pairs   res   mpi   exception   array   move   

Delphi XE10,Json 生成和解析,再利用indyhttp控件Post
2017年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, pdInput, 100, SbrandCode);
          Parameters.CreateParameter(vchineseName, ftString, pdInput, 100, SchineseName);
          Parameters.CreateParameter(venglishName, ftString, pdInput, 100, SenglishName);
          Parameters.CreateParameter(vlogoUrl, ftString, pdInput, 100, SlogoUrl);
          Parameters.CreateParameter(vintroduction, ftString, pdInput, 100, 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, 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;



---------------------------------------------------------------------------------------------------------------

对于数值类型就需要使用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

标签:text   cat   remove   pairs   res   mpi   exception   array   move   

原文地址:https://www.cnblogs.com/westsoft/p/9153722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!