一、用Delphi10.3构造一个JSON数据的第三种方法,并格式化输出,代码如下: uses // System.JSON, System.JSON.Types, System.JSON.Writers, System.JSON.Builders; procedure TForm1.Button3Click(Sender: TObject); var m_StringWriter: TStringWriter; m_JsonWriter: TJsonTextWriter; m_JsonBuilder: TJSONObjectBuilder; begin m_StringWriter := TStringWriter.Create(); m_JsonWriter := TJsonTextWriter.Create(m_StringWriter); m_JsonBuilder := TJSONObjectBuilder.Create(m_JsonWriter); try m_JsonWriter.Formatting := TJsonFormatting.Indented; //JSON构造 m_JsonBuilder .BeginObject .BeginArray('五班') .BeginObject .Add('姓名', '张三') .Add('成绩', 75.5) .EndObject .BeginObject .Add('姓名', '李四') .Add('成绩', 21.7). EndObject .EndArray .EndObject; //输出 Memo1.Lines.Clear; Memo1.Lines.Add(m_StringWriter.ToString); finally m_JsonBuilder.Free; m_JsonWriter.Free; m_StringWriter.Free; end; end; 二、输出结果如下: { "五班": [ { "姓名": "张三", "成绩": 75.5 }, { "姓名": "李四", "成绩": 21.7 } ] }
不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢。
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论