简体   繁体   English

如何使用数据编辑json文件并保存

[英]How to edit json file with data and save it

I have a json file with a lot of objects. 我有一个包含许多对象的json文件。 This file is located in res/raw. 该文件位于res / raw中。 Im reading this file and showing objects in list in my UI. 我正在读取此文件并在UI中的列表中显示对象。 And after user closing activity with this list, changes that user made most be saved in file that was read. 在用户使用此列表关闭活动之后,该用户最常进行的更改将保存在已读取的文件中。

If your file is located inside of your apk file, you cannot change it. 如果您的文件位于apk文件中,则无法更改。 Instead of that have a flag stored in persistent store. 取而代之的是在永久性存储中存储一个标志。 If it is a first run of your application, then read this json file from apk, process it and store it in the device filesystem. 如果它是您的应用程序的首次运行,请从apk读取此json文件,对其进行处理并将其存储在设备文件系统中。

And every next run, check the flag, if it is not the first run, read the file from the filesystem and process it further. 然后,在每次下一次运行时,请检查该标志(如果不是第一次运行),请从文件系统中读取文件并进行进一步处理。

这是使事物静态化的一种坏方法,但是如果对您来说太紧急,则可以读取一次json文件并将其转换为java model(pojo)对象,并使该对象静态化,当您下次访问相同活动时,从该对象加载ui静态对象,而不是再次读取和解析它,为安全起见,您可以检查pojo类对象是否为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM