search
HomeJavaDefine 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"
    }
  }
}

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"
    }

The classes I defined are as follows:

class citydata {

map <string, languagedata> locale;
}

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

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

class city {

map<string, citydata> city;

}

However, I get the following error:

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

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></string>, 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>>() {});

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

{
  "city": {
    "Berlin": { ... },
    "Paris": { ... }
  }
}

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

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
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.