首页 > 代码库 > delphi解析json(解决乱码问题)

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=http://www.mamicode.com/‘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(解决乱码问题)