java 如何使用java重命名JSONObject中的键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29070865/
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
How to rename key in JSONObject using java?
提问by Puneet Purohit
I want to rename the keys of a JSON object using Java.
我想使用 Java 重命名 JSON 对象的键。
My input JSON is:
我的输入 JSON 是:
{
"serviceCentreLon":73.003742,
"type":"servicecentre",
"serviceCentreLat":19.121737,
"clientId":"NMMC01"
}
I want to change it to:
我想把它改成:
{
"longitude":73.003742,
"type":"servicecentre",
"latitude":19.121737,
"clientId":"NMMC01"
}
i.e. I want to rename "serviceCentreLon"
to "longitude"
and "serviceCentreLat"
to "latitude"
. I am using the JSONObject
type in my code.
即我要重新命名"serviceCentreLon"
,以"longitude"
和"serviceCentreLat"
到"latitude"
。我JSONObject
在我的代码中使用了类型。
回答by user253751
Assuming you're using the json.org library: once you have a JSONObject, why not just do this?
假设您正在使用 json.org 库:一旦您有了 JSONObject,为什么不直接这样做呢?
obj.put("longitude", obj.get("serviceCentreLon"));
obj.remove("serviceCentreLon");
obj.put("latitude", obj.get("serviceCentreLat"));
obj.remove("serviceCentreLat");
You could create a rename
method that does this (then call it twice), but that's probably overkill if these are the onlyfields you're renaming.
您可以创建一个rename
执行此操作的方法(然后调用它两次),但如果这些是您要重命名的唯一字段,则这可能有点过头了。
回答by Danyal Sandeelo
String data= json.toString();
data=data.replace("serviceCentreLon","longitude");
data=data.replace("serviceCentreLat","latitude");
convert back to json object
转换回json对象
回答by northernman
To build on Danyal Sandeelo's approach, instead of:
以 Danyal Sandeelo 的方法为基础,而不是:
data=data.replace("serviceCentreLon","longitude");
use
利用
data=data.replace("\"serviceCentreLon\":","\"longitude\":");
This method explicitly matches the json key syntax, and avoids obscure errors where the key value is present as valid data elsewhere in the json string.
此方法显式匹配 json 键语法,并避免了键值在 json 字符串中的其他位置作为有效数据出现的模糊错误。
回答by Edi G.
I'm not sure whether I get your question right, but shouldn't the following work?
我不确定我的问题是否正确,但以下不应该起作用吗?
You could use a regular expression to replace the keys, for example:
您可以使用正则表达式来替换键,例如:
String str = myJsonObject.toString();
str = str.replace(/"serviceCentreLon":/g, '"longitude":');
str = str.replace(/"serviceCentreLat":/g, '"latitude":');
It's not as "clean", but it might get the job done fast.
它不是那么“干净”,但它可以快速完成工作。
回答by vijayakanth p madhavan
The best way to approach the problem is to parse the JSON data and then replace the key. A number of parsers are available - google gson, Hymanson serializer de-serializers, org.json.me are a few such java libraries to handle JSON data.
解决该问题的最佳方法是解析 JSON 数据,然后替换密钥。有许多解析器可用 - google gson、Hymanson 序列化器反序列化器、org.json.me 是一些处理 JSON 数据的此类 Java 库。
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
is a good way to deal with it if you have a pretty generic and relatively huge JSON data. Of course, you have to spend time in learning the library and how to use it well.
如果您有一个非常通用且相对庞大的 JSON 数据,这是处理它的好方法。当然,你必须花时间学习图书馆以及如何用好它。
http://www.baeldung.com/Hymanson-mapis another such parser.
http://www.baeldung.com/Hymanson-map是另一个这样的解析器。
https://stleary.github.io/JSON-java/is the simplest one especially if you don't want any serious serialization or deserialization
https://stleary.github.io/JSON-java/是最简单的一个,特别是如果你不想要任何严重的序列化或反序列化
回答by prasad vsv
Have an object that maps to this object data structure.
有一个映射到这个对象数据结构的对象。
Use GSON parser or Hymanson parser to convert this json into POJO.
使用 GSON 解析器或 Hymanson 解析器将此 json 转换为 POJO。
Then map this object to another Java Object with required configuration
然后将此对象映射到另一个具有所需配置的 Java 对象
Convert that POJO back to json using the same GSON parsers.
使用相同的 GSON 解析器将该 POJO 转换回 json。
refer this for further reference
参考这个进一步参考
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/