在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、我们定义一个简单的类 TPeople = class private FName: string; FScore: Integer; FAge: TDateTime; public property Name: string read FName write FName; property Score: Integer read FScore write FScore; property Age: TDateTime read FAge write FAge; end;
二、我们写下如下Delphi代码: uses REST.Json, System.Json, System.Types, System.Json.Types, System.Json.Writers, System.Json.Builders; procedure TForm1.Button5Click(Sender: TObject); var I: Integer; m_Str: string; m_People: TPeople; begin Memo1.Clear; Memo2.Clear; //实例化类 m_People := TPeople.Create; try m_People.Name := '张三'; m_People.Age := Now; m_People.Score := 98; //类转JSON m_Str := TJson.ObjectToJsonString(m_People); finally m_People.Free; end; //输出JSON Memo1.Lines.Add(m_Str); //JSON转类 m_People := TJson.JsonToObject<TPeople>(m_Str); try //输出类 Memo2.Lines.Add(Format('姓名:%s,分数:%d,年龄:%s', [m_People.Name, m_People.Score, FormatDateTime('yyyy-mm-dd', m_People.Age)])); finally m_People.Free; end; end;
三、显示效果如下:
不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢。
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论