英文:
editing json in internal storage deletes array
问题
我试图更改位于内部存储中的 JSON 中的单个值,JSON 具有 222 个对象,但当我尝试更改一个值时,它们都被删除,只剩下一个。有人知道为什么吗?我尝试像这样更改值:
fun writeJsonToInternalStorage(pos: Int) {
try {
val file = File(this.filesDir, "stempel.json")
if (file.exists()) {
val jsonString = file.readText()
val jsonarr = JSONArray(jsonString)
val jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus", 1)
file.writeText(jsonObject.toString())
}
} catch (e: IOException) {
e.printStackTrace()
}
}
如果我这样做,它会从这个:
[
{
"id": 1,
"sname": "Eckertalsperre (Staumauer)",
"location": "N51.84165 E10.57998",
"sammelstatus": 0
},
{
"id": 2,
"sname": "Scharfenstein (Rangerstation)",
"location": "N51.83017 E10.60277",
"sammelstatus": 0
},
{
"id": 222,
"sname": "Bergbaulehrpfad Wettelrode",
"location": "N51.52038 E11.27694",
"sammelstatus": 0
}
]
变成这样:
{"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:
fun writeJsonToInternalStorage(pos: Int) {
try {
val file = File(this.filesDir, "stempel.json")
if (file.exists()) {
val jsonString = file.readText()
var jsonarr = JSONArray(jsonString)
var jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus",1)
file.writeText(jsonObject.toString())
}
} catch (e: IOException) {
e.printStackTrace()
}
}
if i do this it changes from this:
[
{
"id": 1,
"sname": "Eckertalsperre (Staumauer)",
"location": "N51.84165 E10.57998",
"sammelstatus": 0
},
{
"id": 2,
"sname": "Scharfenstein (Rangerstation)",
"location": "N51.83017 E10.60277",
"sammelstatus": 0
},
{
"id": 222,
"sname": "Bergbaulehrpfad Wettelrode",
"location": "N51.52038 E11.27694",
"sammelstatus": 0
}
]
to this:
{"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
val jsonString = file.readText()
var jsonarr = JSONArray(jsonString)
这段代码正在读取一个 JSON 数组。
var jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus", 1)
file.writeText(jsonObject.toString())
这段代码正在写出一个 JSON 对象,这个对象是从你读取的 JSON 数组中获取的。
所以,尝试:
var jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus", 1)
file.writeText(jsonarr.toString())
这会修改 JSON 数组中的一个 JSON 对象,然后写出整个数组。
英文:
val jsonString = file.readText()
var jsonarr = JSONArray(jsonString)
This code is reading in a JSON array.
var jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus",1)
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:
var jsonObject = jsonarr.getJSONObject(pos)
jsonObject.put("sammelstatus",1)
file.writeText(jsonarr.toString())
This modifies a JSON object inside that JSON array, but then writes out the array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论