How to serialize JSON object in custom getter?
up vote
2
down vote
favorite
I want to get this json from class User:
{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}
This is the class:
class User {
private String id;
private String campaignId;
public String getId() {
return id;
}
public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}
}
But I have a wrong json:
{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}
As you see, first level campaignId is a string instead of an object containing a campaignId key.
How can I achieve that without creating another POJO?
java json jackson
|
show 1 more comment
up vote
2
down vote
favorite
I want to get this json from class User:
{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}
This is the class:
class User {
private String id;
private String campaignId;
public String getId() {
return id;
}
public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}
}
But I have a wrong json:
{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}
As you see, first level campaignId is a string instead of an object containing a campaignId key.
How can I achieve that without creating another POJO?
java json jackson
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
1
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to get this json from class User:
{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}
This is the class:
class User {
private String id;
private String campaignId;
public String getId() {
return id;
}
public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}
}
But I have a wrong json:
{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}
As you see, first level campaignId is a string instead of an object containing a campaignId key.
How can I achieve that without creating another POJO?
java json jackson
I want to get this json from class User:
{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}
This is the class:
class User {
private String id;
private String campaignId;
public String getId() {
return id;
}
public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}
}
But I have a wrong json:
{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}
As you see, first level campaignId is a string instead of an object containing a campaignId key.
How can I achieve that without creating another POJO?
java json jackson
java json jackson
asked Nov 9 at 14:56
Héctor
9,0541357122
9,0541357122
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
1
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03
|
show 1 more comment
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
1
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
1
1
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
I want to get this json from class User: ...
"campaignId" : {
"campaignId" : "whatever"
...
private String campaignId;
Those things simply don't go together.
If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.
In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.
Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.
Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
add a comment |
up vote
-1
down vote
I solved returning Map instead of String:
public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}
That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I want to get this json from class User: ...
"campaignId" : {
"campaignId" : "whatever"
...
private String campaignId;
Those things simply don't go together.
If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.
In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.
Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.
Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
add a comment |
up vote
2
down vote
accepted
I want to get this json from class User: ...
"campaignId" : {
"campaignId" : "whatever"
...
private String campaignId;
Those things simply don't go together.
If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.
In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.
Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.
Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I want to get this json from class User: ...
"campaignId" : {
"campaignId" : "whatever"
...
private String campaignId;
Those things simply don't go together.
If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.
In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.
Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.
Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.
I want to get this json from class User: ...
"campaignId" : {
"campaignId" : "whatever"
...
private String campaignId;
Those things simply don't go together.
If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.
In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.
Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.
Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.
answered Nov 9 at 15:31
GhostCat
85.9k1682141
85.9k1682141
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
add a comment |
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
2
2
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
Disclaimer: no animals were harmed for the production of this answer.
– GhostCat
Nov 9 at 15:31
add a comment |
up vote
-1
down vote
I solved returning Map instead of String:
public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}
That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.
add a comment |
up vote
-1
down vote
I solved returning Map instead of String:
public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}
That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.
add a comment |
up vote
-1
down vote
up vote
-1
down vote
I solved returning Map instead of String:
public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}
That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.
I solved returning Map instead of String:
public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}
That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.
answered Nov 9 at 15:26
Héctor
9,0541357122
9,0541357122
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228113%2fhow-to-serialize-json-object-in-custom-getter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58
another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59
You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00
@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02
1
Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03