delphi解析json(解决乱码问题)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi解析json(解决乱码问题)相关的知识,希望对你有一定的参考价值。

unit Unit2;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.ListView.Types,
FMX.ListView.Appearances, FMX.ListView.Adapters.Base, FMX.ListView,json,
FMX.ScrollBox, FMX.Memo, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP;

type
TForm2 = class(TForm)
Button1: TButton;
Memo1: TMemo;
IdHTTP1: TIdHTTP;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.Button1Click(Sender: TObject);
function getJSONValue(jsonstr: string; jsonvalue:string): string;
var
jo: tjsonobject;
jv: tjsonvalue;
begin
jo := nil;
try
jo := tjsonobject.Create;
jo := tjsonobject.ParseJSONValue(tencoding.UTF8.GetBytes(jsonstr), 0)
as tjsonobject;
jv := jo.Get(jsonvalue).jsonvalue;
Result := jv.Value;
finally
jo.Free;
end;
end;

var
IdHTTP: TIdHTTP;
RequestURL: string;
ResponseStream: TStringStream;
JO, JData: TJSONObject;
kkk,s:string;
LJsonArr : TJSONArray;
LJsonValue : TJSONValue;
LItem : TJSONValue;
begin
IdHTTP := TIdHTTP.Create(nil);
IdHTTP.ReadTimeout := 0;
IdHTTP.AllowCookies := True;
IdHTTP.ProxyParams.BasicAuthentication := False;
IdHTTP.ProxyParams.ProxyPort := 0;
IdHTTP.Request.ContentLength := -1;
IdHTTP.Request.ContentRangeEnd := 0;
IdHTTP.Request.ContentRangeStart := 0;
idhttp.Request.ContentType := ‘application/x-www-form-urlencoded‘;
idhttp.Request.CharSet := ‘utf-8‘;
IdHTTP.Request.Accept := ‘*/*‘;
IdHTTP.Request.BasicAuthentication := False;
IdHTTP.Request.UserAgent := ‘Mozilla/3.0 (compatible; Indy Library)‘;
IdHTTP.HTTPOptions := [hoForceEncodeParams];
RequestURL := ‘http://localhost:8080/userinfo/aaa.do‘;
ResponseStream := TStringStream.Create(‘‘, TEncoding.GetEncoding(65001));

IdHTTP.Get(RequestURL, ResponseStream);
IdHTTP.Free;
kkk:=ResponseStream.DataString;
ResponseStream.Position := 0;
LJsonArr := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(kkk),0) as TJSONArray;
for LJsonValue in LJsonArr do
begin
for LItem in TJSONArray(LJsonValue) do
begin
if TJSONPair(LItem).JsonString.Value=‘userNumber‘ then
begin
S :=Format(‘%s : %s‘,[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]);
showmessage(s);
end;
end;
end;
Memo1.Text := ResponseStream.DataString;


JO.Free;
ResponseStream.Free;
end;

end.















































































以上是关于delphi解析json(解决乱码问题)的主要内容,如果未能解决你的问题,请参考以下文章

如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

hive中文乱码-解析json

Delphi xe5 控件TIdhttp的用法post,get解决中文乱码问题

如何解决Delphi连接Mysql5.0出现乱码的问题

delphi乱码问题

Delphi 编写的软件,文字乱码