我正在使用spring-boot开发应用程序,我需要合并两个对象,这意味着只将新值复制到另一个。
我尝试了这个,但它将所有值frm obj1复制到obj2:
public static void copyObj(User c1, User c2) {
//String[] ignoredFields = { "legalForm", "email" };
BeanUtils.copyProperties(c1, c2);
}
我需要的是只将不同的值从c1复制到c2。 这是一个片段:
**User c1**
{
"name": "LALA",
"email": "tst@tst.com"
}
**user c2**
{
"name": "tata",
"email": "email@tst.fr"
"firsName": "toto"
"city": "Paris"
}
**expected result:**
{
"name": "LALA",
"email": "tst@tst.com"
"firsName": "toto",
"city": "Paris"
}
上述方法返回:
{
"name": "LALA",
"email": "tst@tst.com"
}
你有什么想法吗?
祝你好运
答案 0 :(得分:0)
以下是其中一种方法:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonParser {
JFileChooser chooser = new JFileChooser();
File f;
static String fn = "";
static String js1 = "{\"name\": \"LALA\", \"email\": \"tst@tst.com\"}";
static String js2 = "{\"name\": \"tata\", \"email\": \"email@tst.fr\", \"firsName\": \"toto\", \"city\": \"Paris\"}";
String name = "name";
String email = "email";
String fName = "firsName";
String city = "city";
// ... other needed fields
User u1 = null;
User u2 = null;
User nu = null;
public JsonParser() {
parseFile();
nu = copyObj(u1, u2);
System.out.println("\n" + nu.toShortString());
}
private String openFchooser() throws FileNotFoundException, IOException, InterruptedException, Exception {
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
}
return f.getAbsolutePath();
}
// To parse JSON files with data
//===========================================
public void parseFile() {
JSONParser parser = new JSONParser();
try {
// fn = openFchooser();
// Object obj = parser.parse(new FileReader(fn));
// To parse obj 1
Object obj1 = parser.parse(js1);
System.out.println("User 1: " + obj1.toString());
System.out.println();
JSONObject jobj1 = (JSONObject) obj1;
String from_name = jobj1.get(name).toString();
String from_email = jobj1.get(email).toString();
// String from_fName = jobj1.get(fName).toString();
// String from_city = jobj1.get(city).toString();
u1 = new User(from_name, from_email, null, null);
// System.out.println(u1.toString() + "\n");
Object obj2 = parser.parse(js2);
System.out.println("User 2: " + obj2.toString());
JSONObject jobj2 = (JSONObject) obj2;
from_name = jobj2.get(name).toString();
from_email = jobj2.get(email).toString();
String from_fName = jobj2.get(fName).toString();
String from_city = jobj2.get(city).toString();
u2 = new User(from_name, from_email, from_fName, from_city);
// System.out.println(u2.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JsonParser();
}
public User copyObj(User u1, User u2) {
User nuser = new User(null, null, null, null);
String fn1 = u1.getFirsName();
String fn2 = u2.getFirsName();
boolean hasUppercase1 = !fn1.equals(fn1.toLowerCase());
boolean hasLowercase1 = !fn1.equals(fn1.toUpperCase());
boolean hasUppercase2 = !fn2.equals(fn2.toLowerCase());
boolean hasLowercase2 = !fn2.equals(fn2.toUpperCase());
// To do something needed
if (hasUppercase1 && hasLowercase2) {
nuser.setFirsName(u1.name);
nuser.setEmail(u1.email);
}
if (hasLowercase1 && hasUppercase2) {
nuser.setFirsName(u2.name);
nuser.setEmail(u2.email);
}
// also some conditions
return nuser;
}
class User {
String name = null;
String email = null;
String fName = null;
String city = null;
public User(String n, String e, String f, String c) {
this.name = n;
this.email = e;
this.fName = f;
this.city = c;
}
public String getFirsName() {
return this.name;
}
public String setFirsName(String s) {
return this.name = s;
}
public String getEmail() {
return this.email;
}
public String setEmail(String s) {
return this.email = s;
}
public String toString() {
return "{\"name\":" + this.name + ", "
+ "\"email\":" + this.email + ", "
+ "\"firsName\":" + this.fName + ", "
+ "\"city\":" + this.city + "\"}";
}
public String toShortString() {
return "{\"name\": \"" + this.name + "\", "
+ "\"email\": \"" + this.email + "\"}";
}
};
}
<强>输出:强>
User 1: {"name":"LALA","email":"tst@tst.com"}
User 2: {"city":"Paris","firsName":"toto","name":"tata","email":"email@tst.fr"}
{"name": "LALA", "email": "tst@tst.com"}
如果您需要其他条件,可以在copyObj()
方法中执行此操作。祝你好运!
答案 1 :(得分:-1)
您可以使用此方法进行复制。 这只是为了一个attr做同样的用户
中的所有其他attrpublic void cpyUser(User u1,User u2){
String firstname1 = u1.getFirstName();
String firstname2 = u2.getFistName();
if(!firstname1.equals(firstname2)){
u1.setFirstName(first2);
updateUser(u1);
}
}