在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、我们有一个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)哦,谢谢。 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论