如何使用Java重命名JSONObject中的密钥? [英] How to rename key in JSONObject using java?
本文介绍了如何使用Java重命名JSONObject中的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Java重命名JSON对象的键.
I want to rename the keys of a JSON object using Java.
我输入的JSON是:
{
"serviceCentreLon":73.003742,
"type":"servicecentre",
"serviceCentreLat":19.121737,
"clientId":"NMMC01"
}
我想将其更改为:
{
"longitude":73.003742,
"type":"servicecentre",
"latitude":19.121737,
"clientId":"NMMC01"
}
即我想将"serviceCentreLon"
重命名为"longitude"
,将"serviceCentreLat"
重命名为"latitude"
.我在代码中使用了JSONObject
类型.
i.e. I want to rename "serviceCentreLon"
to "longitude"
and "serviceCentreLat"
to "latitude"
. I am using the JSONObject
type in my code.
推荐答案
假设您正在使用json.org库:拥有JSONObject之后,为什么不这样做呢?
Assuming you're using the json.org library: once you have a JSONObject, why not just do this?
obj.put("longitude", obj.get("serviceCentreLon"));
obj.remove("serviceCentreLon");
obj.put("latitude", obj.get("serviceCentreLat"));
obj.remove("serviceCentreLat");
您可以创建一个执行此操作的rename
方法(然后将其调用两次),但是如果这些是您要重命名的 only 字段,则可能会过大.
You could create a rename
method that does this (then call it twice), but that's probably overkill if these are the only fields you're renaming.
这篇关于如何使用Java重命名JSONObject中的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文