I have been looking solution for this problem but could not find one so asking this question.
I have some data which looks like this
我一直在寻找解决这个问题的办法,但是没有找到,所以就问这个问题。我有一些像这样的数据
{
"data": [
{
"id": "5ab892c71810e201e81b9d39",
"isSignedUpUsingFb": false,
"personalInformation": {
"firstName": "jio",
"lastName": "g",
"mobileNumber": "1234567890",
},
"accountBalance": 0,
}
]
},
I want to write a java code to change the data structure to this
我想编写一个java代码将数据结构更改为这个
{
"data": [
{
"id": "5ab892c71810e201e81b9d39",
"isSignedUpUsingFb": false,
"personalInformation_firstName":"jio",
"personalInformation_lastNAme":"g",
"personalInformation_mobileNumber":"1234567890",
"accountBalance": 0,
}
]
},
I am getting data from db as:
我从db得到的数据是:
@Override
public List<User> getAllUsers() {
logger.debug("entering all users method");
List<User> allUsers=mongoOperations.findAll(User.class);
for (User user : allUsers) {
PersonalInformation info=user.getPersonalInformation());
//manipulation code here
user.setPersonalInformation(info);
}
return allUsers;
}
So I want to write a logic so that i can convert the data in desired format and send it a return type. I know how to do same thing using J query but I want to do it in backend so any code for the above or any link will help.
所以我想写一个逻辑,这样我就可以把数据转换成想要的格式并发送一个返回类型。我知道如何使用J查询做同样的事情,但是我想在后端做它,所以上面的任何代码或任何链接都会有所帮助。
2 个解决方案
#1
1
I have fond one solution which is very simple.So, basically when we create object for nested data we create it like this in JAVA.
我有一个很简单的解。基本上,当我们为嵌套数据创建对象时我们在JAVA中创建它。
public MyClass{
public String name;
public String contact;
public PersonalInformation personalinformation;
//setters and getter here
}
this will give me data as
这会给我数据a
"MyClass":{
"name": "abc",
"contact": "12345",
"personalInformation":{
"address": "asdasdasdad",
"city":"asdadad",
"pin": "asdfg",
}
}
so to remove this nested data we need to use @JsonUnwrapped
which removes all the nested object and add it to our main object.
因此,要删除这个嵌套数据,我们需要使用@ jsonunwrap,它将删除所有嵌套对象并将其添加到我们的主对象中。
public MyClass{
public String name;
public String contact;
@JsonUnwrapped
public PersonalInformation personalinformation;
//setters and getter here
}
which will change the data structure as:
将改变数据结构为:
"MyClass":{
"name": "abc",
"contact": "12345",
"address": "asdasdasdad",
"city":"asdadad",
"pin": "asdfg",
}
for more reference you can check this link http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html
Hope this helps.
要获得更多的引用,可以查看这个链接http://fasterxml.github.io/jackson- annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/jsonunwrap。html希望这有助于。
#2
0
There are multiple possible solutions. As Prabhav has mentioned the most intuitive one would be to create a new class and from there a object which can be transformed with a library to a JSON.
有多种可能的解决方案。正如Prabhav所提到的,最直观的方法是创建一个新类,然后从这个类创建一个对象,该对象可以用库转换为JSON。
Variant one:
变体:
The new class would look like your data structure you want and access would be:
新的类看起来像您想要的数据结构,访问的对象是:
PersonalInformationJson pf = new PersonalInformationJson();
pf.setFirstName = info.getPersonalInformation_firstName
//... setting the rest of the object
//using jackson
ObjectMapper mapper = new ObjectMapper();
try {
// convert user object to json string and return it
String jsonString = mapper.writeValueAsString(u);
}
The other easier version to create a string, either per hand or use a lib:
另一个更简单的版本是创建一个字符串,或者是用手或者是使用lib:
// using org.json.JSONObject
String jsonString = new JSONObject().put("personalInformation_firstName", info.value())
.put("personalInformation_lastNAme", info.value());
#1
1
I have fond one solution which is very simple.So, basically when we create object for nested data we create it like this in JAVA.
我有一个很简单的解。基本上,当我们为嵌套数据创建对象时我们在JAVA中创建它。
public MyClass{
public String name;
public String contact;
public PersonalInformation personalinformation;
//setters and getter here
}
this will give me data as
这会给我数据a
"MyClass":{
"name": "abc",
"contact": "12345",
"personalInformation":{
"address": "asdasdasdad",
"city":"asdadad",
"pin": "asdfg",
}
}
so to remove this nested data we need to use @JsonUnwrapped
which removes all the nested object and add it to our main object.
因此,要删除这个嵌套数据,我们需要使用@ jsonunwrap,它将删除所有嵌套对象并将其添加到我们的主对象中。
public MyClass{
public String name;
public String contact;
@JsonUnwrapped
public PersonalInformation personalinformation;
//setters and getter here
}
which will change the data structure as:
将改变数据结构为:
"MyClass":{
"name": "abc",
"contact": "12345",
"address": "asdasdasdad",
"city":"asdadad",
"pin": "asdfg",
}
for more reference you can check this link http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html
Hope this helps.
要获得更多的引用,可以查看这个链接http://fasterxml.github.io/jackson- annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/jsonunwrap。html希望这有助于。
#2
0
There are multiple possible solutions. As Prabhav has mentioned the most intuitive one would be to create a new class and from there a object which can be transformed with a library to a JSON.
有多种可能的解决方案。正如Prabhav所提到的,最直观的方法是创建一个新类,然后从这个类创建一个对象,该对象可以用库转换为JSON。
Variant one:
变体:
The new class would look like your data structure you want and access would be:
新的类看起来像您想要的数据结构,访问的对象是:
PersonalInformationJson pf = new PersonalInformationJson();
pf.setFirstName = info.getPersonalInformation_firstName
//... setting the rest of the object
//using jackson
ObjectMapper mapper = new ObjectMapper();
try {
// convert user object to json string and return it
String jsonString = mapper.writeValueAsString(u);
}
The other easier version to create a string, either per hand or use a lib:
另一个更简单的版本是创建一个字符串,或者是用手或者是使用lib:
// using org.json.JSONObject
String jsonString = new JSONObject().put("personalInformation_firstName", info.value())
.put("personalInformation_lastNAme", info.value());