将复选框标题和状态存储为JSON

时间:2023-01-14 21:17:07

I am storing check boxes title and state as JSON having a JSONArray of JSONObject containing these values.

我将复选框标题和状态存储为具有包含这些值的JSONA的JSONArray的JSON。

I tried to store it as two JSONArrrays one for title and one for state, but I am not able to write an algorithm to maintain these with the states as well as removing any one from the list.

我试图将它存储为两个JSONArrrays,一个用于标题,一个用于状态,但我无法编写算法来维护这些状态以及从列表中删除任何一个。

It is taking too much lines of code to do it. Making methods to delete elements of array containing boolean and other containing string.

这样做需要太多代码。制作方法删除包含boolean和其他包含字符串的数组元素。

Also we can't use Jsonarray.remove() method in older apis. So how would you suggest to achieve this efficiently

此外,我们不能在旧的apis中使用Jsonarray.remove()方法。那么你如何建议有效地实现这一目标

Note - I can't use sharedPreferences as I am storing multiple rows with this type of data. It is like a checklist maker where we can add or remove list items and can mark it as complete when done.

注意 - 我不能使用sharedPreferences,因为我使用这种类型的数据存储多行。它就像一个清单制造商,我们可以在其中添加或删除列表项,并在完成后将其标记为完整。

I want to store in database as well and retrieve them when required, I chose JSON as it can be stored as string and can be parsed into object when required

我想在数据库中存储并在需要时检索它们,我选择了JSON,因为它可以存储为字符串,并且可以在需要时解析为对象

1 个解决方案

#1


0  

You have lots of options. If you are using a database just create a schema like,

你有很多选择。如果您正在使用数据库,只需创建一个类似的架构,

checkbox_key TEXT NOT NULL,
checkbox_value INT DEFAULT 0

You certainly can use shared prefs. Just formulate your schema like,

你当然可以使用共享的首选项。只需制定您的架构,就像

checkbox.<checkbox-key>=<checkbox-value>

JSON isn't a great choice since parsing it a relatively expensive operation. Shared prefs, while stored as XML, are optimized with an in-memory cache and async writes (see Editor.apply()). You could do that with your JSON but it's just re-inventing the wheel.

JSON不是一个很好的选择,因为解析它是一个相对昂贵的操作。共享首选项虽然存储为XML,但通过内存缓存和异步写入进行了优化(请参阅Editor.apply())。你可以用你的JSON做到这一点,但它只是重新发明*。

#1


0  

You have lots of options. If you are using a database just create a schema like,

你有很多选择。如果您正在使用数据库,只需创建一个类似的架构,

checkbox_key TEXT NOT NULL,
checkbox_value INT DEFAULT 0

You certainly can use shared prefs. Just formulate your schema like,

你当然可以使用共享的首选项。只需制定您的架构,就像

checkbox.<checkbox-key>=<checkbox-value>

JSON isn't a great choice since parsing it a relatively expensive operation. Shared prefs, while stored as XML, are optimized with an in-memory cache and async writes (see Editor.apply()). You could do that with your JSON but it's just re-inventing the wheel.

JSON不是一个很好的选择,因为解析它是一个相对昂贵的操作。共享首选项虽然存储为XML,但通过内存缓存和异步写入进行了优化(请参阅Editor.apply())。你可以用你的JSON做到这一点,但它只是重新发明*。