• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Using json key as a field in java object

Resolved: Using json key as a field in java object

0
By Isaac Tonny on 15/03/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve Using json key as a field in java object

Question:

I have the following json that I want to serialize and deserialize.
Is it possible to use a key (eg. “name1” instead of “properites”) as the name property in the java object either with annotation or with custom (de)serializer?
The result without any config is the following:

Best Answer:

If the JSON property "name1" is just an example and can vary (not known ahead of time), this might be what you’re looking for:
The output of that is:
The keys to this working are @JsonAnyGetter and @JsonAnySetter which are generally for serializing/deserializing arbitrary properties that don’t have dedicated, named fields or getters.
By using @JsonIgnore on the fields themselves, this allows the “dynamic” properties to shine through instead of the internal representation of the Java object.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

jackson java json
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed. cors error not resolving

02/04/2023

Resolved: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘crm.email’ doesn’t exist (Connection: mysql, SQL: select count(*) as aggregate from `email`

02/04/2023

Resolved: Regex: Curly braces in reverse? Match X characters before a string?

02/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.