百度获取图片 json格式解析
var h,i: integer; ss, url: string; mem: TMemoryStream; str1: tstringlist; memstr: TStringStream; iddec: TIdEncoderMIME; JSONObject,JSONObject2: TJSONObject; // JSON类 jsonArray: TJSONArray; // JSON数组变量 temp: string; // 临时使用变量 begin token := '24.49a7c8acdbf69413ce0f998ad39a7a3d.2592000.1548050649.282335-9533039'; //图片获取的token if token = '' then Application.MessageBox('请输入token', '提示', MB_OK + MB_ICONINFORMATION + MB_TOPMOST) else if cxImage1.Picture.Width = 0 then Application.MessageBox('请先载入图片', '提示', MB_OK + MB_ICONSTOP + MB_TOPMOST) else begin iddec := TIdEncoderMIME.Create; screen.Cursor := crhourglass; // ssl.SSLOptions.Method:= sslvSSLv3; memstr := TStringStream.Create; mem := TMemoryStream.Create; str1 := tstringlist.Create; try cxImage1.Picture.SaveToStream(mem); mem.Position := 0; ss := iddec.Encode(mem); // ******* mem.Free; //提交 str1.Add('image=' + URLEncode(ss)); url := 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token='; // https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token= // https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic ?access_token= url := url + token; RESTRequest1.AddParameter('image', ss); //传送一个图片的流 RESTClient1.BaseURL := url; restrequest1.Execute; ss := RESTResponse1.Content; //获取返回值 finally memstr.Free; str1.Free; iddec.Free; screen.Cursor := crdefault; end; JSONObject := nil; JSONObject:=TJSONObject.ParseJSONValue(Trim(ss)) as TJSONObject; if JSONObject.Count > 0 then begin { 1,遍历JSON数据 } mmo2.Lines.Add('遍历JSON数据:' + #13#10); mmo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count)); for i := 0 to JSONObject.Count - 1 do begin if i = 0 then begin temp := JSONObject.Get(i).ToString + #13#10;; end else begin temp := temp + JSONObject.Get(i).ToString + #13#10; end; end; mmo2.Lines.Add(temp); temp := 'words = ' + JSONObject.Values['words_result'].ToString + #13#10; jsonArray := TJSONArray(JSONObject.GetValue('words_result')); if jsonArray.Count > 0 then begin // 得到JSON数组字符串 temp := 'other = ' + JSONObject.GetValue('words_result').ToString + #13#10; // 循环取得JSON数组中每个元素 for i := 0 to jsonArray.Size - 1 do begin JSONObject2:=TJSONObject.ParseJSONValue(Trim(jsonArray.Items[i].ToString)) as TJSONObject; temp := temp + IntToStr(i + 1) + ' : ' + JSONObject2.Values['words'].ToString + #13#10; end; end; Mmo2.Lines.Add(temp); end; // mmo2.Lines.Add(SJson.GetValue('words').ToString); h := pos('words":', ss); if h > 0 then begin while h > 0 do begin delete(ss, 1, h + 6); delete(ss, 1, pos('"', ss)); mmo1.Lines.Add(copy(ss, 1, pos('"', ss) - 1)); h := pos('words":', ss); end; end else mmo1.Lines.Add(ss); end; end;