微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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,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;







个人分类

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐