编辑内部存储中的JSON会删除数组。

huangapple go评论142阅读模式
英文:

editing json in internal storage deletes array

问题

我试图更改位于内部存储中的 JSON 中的单个值,JSON 具有 222 个对象,但当我尝试更改一个值时,它们都被删除,只剩下一个。有人知道为什么吗?我尝试像这样更改值:

  1. fun writeJsonToInternalStorage(pos: Int) {
  2. try {
  3. val file = File(this.filesDir, "stempel.json")
  4. if (file.exists()) {
  5. val jsonString = file.readText()
  6. val jsonarr = JSONArray(jsonString)
  7. val jsonObject = jsonarr.getJSONObject(pos)
  8. jsonObject.put("sammelstatus", 1)
  9. file.writeText(jsonObject.toString())
  10. }
  11. } catch (e: IOException) {
  12. e.printStackTrace()
  13. }
  14. }

如果我这样做,它会从这个:

  1. [
  2. {
  3. "id": 1,
  4. "sname": "Eckertalsperre (Staumauer)",
  5. "location": "N51.84165 E10.57998",
  6. "sammelstatus": 0
  7. },
  8. {
  9. "id": 2,
  10. "sname": "Scharfenstein (Rangerstation)",
  11. "location": "N51.83017 E10.60277",
  12. "sammelstatus": 0
  13. },
  14. {
  15. "id": 222,
  16. "sname": "Bergbaulehrpfad Wettelrode",
  17. "location": "N51.52038 E11.27694",
  18. "sammelstatus": 0
  19. }
  20. ]

变成这样:

  1. {"id":222,"sname":"Bergbaulehrpfad Wettelrode","location":"N51.52038 E11.27694","sammelstatus":1}

我已经问过 ChatGPT 为什么会这样,但他也不能帮助我。从这些 "[]" 来看,我认为我的 JSON 应该是一个数组。

英文:

Im trying to change a single value of my json that is located in the internal storage, the json has 222 objects, but somehow they are all deleted but one when i try to change a value. Does anyone know why this is? I am trying to change the value like this:

  1. fun writeJsonToInternalStorage(pos: Int) {
  2. try {
  3. val file = File(this.filesDir, "stempel.json")
  4. if (file.exists()) {
  5. val jsonString = file.readText()
  6. var jsonarr = JSONArray(jsonString)
  7. var jsonObject = jsonarr.getJSONObject(pos)
  8. jsonObject.put("sammelstatus",1)
  9. file.writeText(jsonObject.toString())
  10. }
  11. } catch (e: IOException) {
  12. e.printStackTrace()
  13. }
  14. }

if i do this it changes from this:

  1. [
  2. {
  3. "id": 1,
  4. "sname": "Eckertalsperre (Staumauer)",
  5. "location": "N51.84165 E10.57998",
  6. "sammelstatus": 0
  7. },
  8. {
  9. "id": 2,
  10. "sname": "Scharfenstein (Rangerstation)",
  11. "location": "N51.83017 E10.60277",
  12. "sammelstatus": 0
  13. },
  14. {
  15. "id": 222,
  16. "sname": "Bergbaulehrpfad Wettelrode",
  17. "location": "N51.52038 E11.27694",
  18. "sammelstatus": 0
  19. }
  20. ]

to this:

  1. {"id":222,"sname":"Bergbaulehrpfad Wettelrode","location":"N51.52038 E11.27694","sammelstatus":0}

Ive asked chatgpt why this is, but he cant help me eather. As seen with these "[]" my Json should be an array i think.

答案1

得分: 1

  1. val jsonString = file.readText()
  2. var jsonarr = JSONArray(jsonString)

这段代码正在读取一个 JSON 数组

  1. var jsonObject = jsonarr.getJSONObject(pos)
  2. jsonObject.put("sammelstatus", 1)
  3. file.writeText(jsonObject.toString())

这段代码正在写出一个 JSON 对象,这个对象是从你读取的 JSON 数组中获取的。

所以,尝试:

  1. var jsonObject = jsonarr.getJSONObject(pos)
  2. jsonObject.put("sammelstatus", 1)
  3. file.writeText(jsonarr.toString())

这会修改 JSON 数组中的一个 JSON 对象,然后写出整个数组。

英文:
  1. val jsonString = file.readText()
  2. var jsonarr = JSONArray(jsonString)

This code is reading in a JSON array.

  1. var jsonObject = jsonarr.getJSONObject(pos)
  2. jsonObject.put("sammelstatus",1)
  3. file.writeText(jsonObject.toString())

This code is writing out a JSON object, one that you obtained from that JSON array that you read in.

So, try:

  1. var jsonObject = jsonarr.getJSONObject(pos)
  2. jsonObject.put("sammelstatus",1)
  3. file.writeText(jsonarr.toString())

This modifies a JSON object inside that JSON array, but then writes out the array.

huangapple
  • 本文由 发表于 2023年6月26日 03:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定