Home > Java > body text

Define POJOs with mutable keys

王林
Release: 2024-02-09 08:42:09
forward
484 people have browsed it

php editor Zimo will introduce you to how to use variable keys to define POJOs (plain old Java objects) in this article. In programming, POJO refers to a simple Java object that does not depend on any specific framework or library. Variable keys mean that when defining a POJO, the keys (property names) can be dynamically modified or expanded as needed. This technology allows us to operate object properties more flexibly and improves the readability and maintainability of code. Next, we will delve into how to define POJOs with variable keys and give some examples of practical application scenarios.

Question content

I am trying to convert json to pojo where the keys are mutable. For example: Berlin, Paris in the following example:

{
  "berlin": {
    "en-us": {
      "displayname": "us",
      "supportedlanguage": [
        "us"
      ],
      "fullexample": "hello us"
    },
    "en-ca": {
      "displayname": "ca",
      "supportedlanguage": [
        "ca"
      ],
      "fullexample": "hello ca"
    }
  },
  "paris": {
    "en-us": {
      "displayname": "us",
      "supportedlanguage": [
        "us"
      ],
      "fullexample": "hello us"
    },
    "en-ca": {
      "displayname": "ca",
      "supportedlanguage": [
        "ca"
      ],
      "fullexample": "hello ca"
    }
  }
}
Copy after login

For everything within the variable key (Berlin, Paris), for example:

"en-us": {
      "displayname": "us",
      "supportedlanguage": [
        "us"
      ],
      "fullexample": "hello us"
    },
    "en-ca": {
      "displayname": "ca",
      "supportedlanguage": [
        "ca"
      ],
      "fullexample": "hello ca"
    }
Copy after login

The classes I defined are as follows:

class citydata {

map <string, languagedata> locale;
}

class languagedata {
string displayname;
list<string> supportedlanguage;
string fullexample;
}
Copy after login

Finally, in order to accommodate variablekeys, I defined a new object as follows:

class city {

map<string, citydata> city;

}
Copy after login

However, I get the following error:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Berlin" , not marked as ignorable (0 known properties: ])
Copy after login

How to store variable keys in pojo? This is something essential so I want to retrieve it via jackson objectmapper readvalue.

Workaround

You need to parse the data as map<string, citydata>, not as an object containing the map attribute. Your root level is the map.

map<string, citydata> parsed = objectmapper.readvalue(
  input,
  new typereference<map<string, citydata>>() {});
Copy after login

Using your type city, you can parse json using the following structure:

{
  "city": {
    "Berlin": { ... },
    "Paris": { ... }
  }
}
Copy after login

Which has your actual json nested under the key "city" (map<string,citydata> city).

The above is the detailed content of Define POJOs with mutable keys. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template