There is any way to copy only the value of equal properties from one JSON to another?
For example:
有没有办法只将相等属性的值从一个JSON复制到另一个JSON ?例如:
json1-
json1 -
{
"isMultiRow": true,
"name": "Donny",
"description": "Donnyboy"
}
json2-
json2 -
{
"isMultiRow": false,
"name": "Jonny",
"description": "Jonny boy"
"age": "old"
"radius":"big"
}
if I do something like json1.copy(json2)
I'll get at json1 -
如果我做一些类似json1.copy(json2)的事情,我会在json1 -
{
"isMultiRow": false,
"name": "Jonny",
"description": "Jonny boy"
}
age
and radius
won't appear because they don't exist in json1.
年龄和半径不会出现,因为它们在json1中不存在。
2 个解决方案
#1
1
You can either write a custom method that accepts 2 JSONObject and a list of fields that needs to be copied from one src JSONObject to dest JSONObject.
您可以编写一个接受2个JSONObject的自定义方法,以及需要从一个src JSONObject复制到dest JSONObject的字段列表。
private static void copy(JSONObject dest, JSONObject src, List<String> fields) {
for (String key : fields) {
dest.put(key, src.get(key));
}
}
Or you can have your own custom class that extends JSONObject and have a new method copy that accepts another JSON and copies field by field.
或者,您可以有自己的自定义类来扩展JSONObject,并有一个接受另一个JSON的新方法副本,并逐个字段地复制。
public class JSON extends JSONObject {
private static final long serialVersionUID = 1L;
public void copy(JSON other) {
// implement copy logic by iterating over keySet etc
}
}
NOTE:
If your JSON has simple key-values then above will be very simple implementation, if your JSON string has complex objects, arrays etc then you would have add handling for each of those types and do a deep copy/override from src to dest.
注意:如果您的JSON有简单的键值,那么上面的实现将非常简单,如果您的JSON字符串有复杂的对象、数组等等,那么您将需要为这些类型添加处理,并从src到dest执行深度复制/覆盖。
#2
0
Assuming the type of your JSON object implements Map
, you can do this:
假设您的JSON对象的类型实现映射,您可以这样做:
json1.replaceAll(json2::getOrDefault);
This will replace fields in json1
with their corresponding values from json2
. If json2
doesn't have a given field, it will be left as is.
这将用json1中的相应值替换json2中的字段。如果json2没有给定的字段,它将保持原样。
#1
1
You can either write a custom method that accepts 2 JSONObject and a list of fields that needs to be copied from one src JSONObject to dest JSONObject.
您可以编写一个接受2个JSONObject的自定义方法,以及需要从一个src JSONObject复制到dest JSONObject的字段列表。
private static void copy(JSONObject dest, JSONObject src, List<String> fields) {
for (String key : fields) {
dest.put(key, src.get(key));
}
}
Or you can have your own custom class that extends JSONObject and have a new method copy that accepts another JSON and copies field by field.
或者,您可以有自己的自定义类来扩展JSONObject,并有一个接受另一个JSON的新方法副本,并逐个字段地复制。
public class JSON extends JSONObject {
private static final long serialVersionUID = 1L;
public void copy(JSON other) {
// implement copy logic by iterating over keySet etc
}
}
NOTE:
If your JSON has simple key-values then above will be very simple implementation, if your JSON string has complex objects, arrays etc then you would have add handling for each of those types and do a deep copy/override from src to dest.
注意:如果您的JSON有简单的键值,那么上面的实现将非常简单,如果您的JSON字符串有复杂的对象、数组等等,那么您将需要为这些类型添加处理,并从src到dest执行深度复制/覆盖。
#2
0
Assuming the type of your JSON object implements Map
, you can do this:
假设您的JSON对象的类型实现映射,您可以这样做:
json1.replaceAll(json2::getOrDefault);
This will replace fields in json1
with their corresponding values from json2
. If json2
doesn't have a given field, it will be left as is.
这将用json1中的相应值替换json2中的字段。如果json2没有给定的字段,它将保持原样。