pierrot_98
[델파이] json parsing 본문
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
uses
.., DBXJSON; //DBXJSON 추가
private
function funcSetJsonObject(psJsonTxT : String) : TJsonObject;
function funcGetJsonParsing(var pSetStringList : TStringList; psJson : TJSONValue) : Boolean;
.....
procedure Button1Click(Sender: TObject);
var
LJsonObj : TJSONValue;
JsonParsing : TStringList;
begin
try
JsonParsing := TStringList.Create;
LJsonObj := funcSetJsonObject(sJsonTxT);
funcGetJsonParsing(JsonParsing, LJsonObj);
Memo1.Text := JsonParsing.Text;
finally
JsonParsing.Free;
end;
end;
//Json을 Json obejct로 변환
function funcSetJsonObject(psJsonTxT : String) : TJsonObject;
var
LJSONBytes: TBytes;
LJSONSObj : TJSONValue;
begin
LJSONBytes := TEncoding.UTF8.GetBytes(psJsonTxT);
LJSONSObj := TJSONObject.ParseJSONValue(LJSONBytes, 0);
Result := TJSONObject(LJSONSObj);
end;
//Json 파싱
function funcGetJsonParsing(var pSetStringList : TStringList psJson : TJSONValue) : Boolean;
var
LJsonArr : TJSONArray;
LJsonValue : TJSONValue;
LItem : TJSONValue;
LJsonObj : TJSONObject;
iLoop : integer;
sKey, sValue : string;
isArray : Boolean;
begin
Result := True;
try
isArray := True;
//Json Object / Array 구분
if (psJson is TJSONObject) then
isArray := True
else if (psJson is TJSONArray) then
isArray := False;
if (isArray) then
begin
LJsonObj := TJSONObject(psJson);
pSetStringList.Clear;
for iLoop := 0 to LJsonObj.Size - 1 do
begin
sKey := LJsonObj.Get(iLoop).JsonString.Value;
sValue := LJsonObj.Get(iLoop).JsonValue.Value;
pSetStringList.Add(sKey + ':' + sValue);
end;
end else
begin
LJsonArr := TJSONArray(psJson);
pSetStringList.Clear;
for LJsonValue in LJsonArr do
begin
for LItem in TJSONArray(LJsonValue) do
begin
//Return Json Array
pSetStringList.Add((Format('%s:%s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value])));
//Json object in object
if TJSONPair(LItem).JsonValue is TJSONObject then
begin
for iLoop := 0 to TJSONArray(TJSONPair(LItem).JsonValue).Size -1 do
begin
pSetStringList.Add(TJSONArray(TJSONPair(LItem).JsonValue).Get(iLoop).ToSTring);
end;
end;
end;
end;
end;
except
Result := False;
end;
end;
|
cs |
델파이 Json 파싱 예제
jsonobject / jsonarray를 구분하여 파싱
'IT > Delphi' 카테고리의 다른 글
[델파이] 레지스트리 등록 (0) | 2020.01.15 |
---|---|
[델파이] TIdHTTP JSON 형식 POST - 바이너리 파일 받아오기 (0) | 2019.10.31 |
[델파이] 현재 모니터 구하기 및 폼 전체 화면 (0) | 2019.10.31 |
[델파이] 마우스 커서 바꾸기 (0) | 2019.09.24 |
[델파이] ShellExecute (0) | 2019.09.24 |
Comments