Table of Contents
Question content
Workaround
Home Java Define POJOs with mutable keys

Define POJOs with mutable keys

Feb 09, 2024 am 08:42 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)