Home  >  Article  >  Define POJOs with mutable keys

Define POJOs with mutable keys

王林
王林forward
2024-02-09 08:42:09431browse

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