首页 > Windows开发 > 详细

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

时间:2017-03-24 18:47:20      阅读:1051      评论:0      收藏:0      [点我收藏+]

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(解决乱码问题)

原文:http://www.cnblogs.com/figofifa/p/6612901.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!