JSON還原爲結構體

JSON還原爲結構體json

1)JSON字符串還原爲結構體;orm

2)訪問結構體的字段值;中間件

本例運行效果圖:blog

uses
SynCommons;字符串

const // JSON字符串
JSON1 =
'{' + #13#10 + '"glossary": {' + #13#10 + '"title": "中國",' + #13#10
+ ' "GlossDiv": {' + #13#10
+ '"title": "湖南省",' + #13#10
+ ' "GlossList": {' + #13#10
+ '"GlossEntry": {' + #13#10
+ '"ID": "湘鄉市",' + #13#10
+ ' "SortAs": "SGML",' + #13#10
+ ' "GlossTerm": "Standard Generalized Markup Language",' + #13#10
+ ' "Acronym": "SGML",' + #13#10
+ ' "Abbrev": "ISO 8879:1986",' + #13#10
+ ' "GlossDef": {' + #13#10
+ '"para": "A meta-markup language, used to create markup languages such as DocBook.",' + #13#10
+ ' "GlossSeeAlso": ["詠南中間件", "XML"]' + #13#10
+ '},' + #13#10
+ ' "GlossSee": "markup"' + #13#10 + '}'
+ #13#10 + '}' + #13#10
+ '}' + #13#10
+ '}' + #13#10
+ '}';string

type // 記錄
TGlossary = record
glossary: record
title: string;
GlossDiv: record
title: string;
GlossList: record
GlossEntry: record
ID, SortAs, GlossTerm, Acronym, Abbrev: string;
GlossDef: record
para: string;
GlossSeeAlso: array of string;
end;
GlossSee: string;
end;
end;
end;
end;
end;it

procedure TForm1.Button1Click(Sender: TObject);
var
gloss: TGlossary;
json: RawUTF8;
begin
json := JSON1;
RecordLoadJSON(gloss, @json[1], TypeInfo(TGlossary));
Memo1.Clear;
Memo1.Lines.Add(gloss.glossary.title); // 中國
Memo1.Lines.Add(gloss.glossary.GlossDiv.title); // 湖南省
Memo1.Lines.Add(gloss.glossary.GlossDiv.GlossList.GlossEntry.ID); // 湘鄉市
Memo1.Lines.Add(gloss.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[0]); // 詠南中間件
end;sed

相關文章
相關標籤/搜索