java 如何更改json对象名称(键)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26065020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:14:11  来源:igfitidea点击:

how to change json object name (key)?

javaandroid

提问by SaDeGH_F

Just a quick question:

只是一个简单的问题:

If I have a json object which contains "key/value"s like this:

如果我有一个包含“键/值”的 json 对象,如下所示:

"name":"value"

and I want to change the name parts to something else, what should I do? I don't want to remove and make it again.

我想将名称部分更改为其他内容,我该怎么办?我不想删除并重新制作。

回答by njzk2

Proper and compact implementation, given that jsonObjectis the json object you are referring to and it is an instance of JSONObject:

正确和紧凑的实现,因为这jsonObject是您所指的 json 对象,它是 的一个实例JSONObject

jsonObject.put("new name", jsonObject.remove("name"));

回答by Pedro Oliveira

Create a new object with the new value

使用新值创建一个新对象

new JSONObject(jsonobject.toString().replace("\"name\":","\"newvalue\":");)