标签:obj phi image array span 10个 begin ext div
一、我们有一个JSON文件,如下:
{ "在野": [ { "城池": 0, "武将": 74, "登场年": 190 }, { "城池": 0, "武将": 11, "登场年": 208 }, { "城池": 29, "武将": 78, "登场年": 198 } ], "跟随": [ { "父将": 182, "子将": 176, "登场年": 197 }, { "父将": 222, "子将": 224, "登场年": 199 } ] }
二、我们读取“在野”,代码如下:
//读取JSON文件内容 procedure TForm1.Button1Click(Sender: TObject); var m_Json, m_Json_Item: TJSONObject; m_StringStream: TStringStream; m_JsonArr: TJSONArray; I: Integer; begin m_StringStream := TStringStream.Create(‘‘, TEncoding.UTF8); m_StringStream.LoadFromFile(Edit1.Text); m_Json := TJSONObject.Create; m_Json := TJSONObject.ParseJSONValue(m_StringStream.DataString) as TJSONObject; m_JsonArr := m_Json.Values[‘在野‘] as TJSONArray; // 第10个 for I := 0 to m_JsonArr.Count - 1 do begin m_Json_Item := m_JsonArr.Items[I] as TJSONObject; Memo1.Lines.Add(Format(‘武将: %s ,登场年: %s ‘, [m_Json_Item.Values[‘武将‘].Value, m_Json_Item.Values[‘登场年‘].Value])); end; end; //修改JSON文件内容 procedure TForm1.Button2Click(Sender: TObject); var m_Json, m_Json_Item: TJSONObject; m_StringStream: TStringStream; m_JsonArr: TJSONArray; begin m_StringStream := TStringStream.Create(‘‘, TEncoding.UTF8); m_StringStream.LoadFromFile(Edit1.Text); //读取JSON m_Json := TJSONObject.Create; m_Json := TJSONObject.ParseJSONValue(m_StringStream.DataString) as TJSONObject; m_JsonArr := m_Json.Values[‘在野‘] as TJSONArray; // 修改JSON数组内容 m_Json_Item := m_JsonArr.Items[2] as TJSONObject; m_Json_Item.RemovePair(‘武将‘); m_Json_Item.AddPair(‘武将‘, ‘520‘); m_Json_Item.RemovePair(‘登场年‘); m_Json_Item.AddPair(‘登场年‘, ‘1314‘); //保存到文件 m_StringStream.Clear; m_StringStream.WriteString(m_Json.ToString); m_StringStream.SaveToFile(Edit1.Text); end;
三、效果如下:
不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢。
八、Delphi10.3读取JSON文件,并修改JSON数组一条内容后保存到文件
标签:obj phi image array span 10个 begin ext div
原文地址:https://www.cnblogs.com/tianpan2019/p/11823676.html