Let's say I have a class like this:
public class Person {
private String firstName;
private String lastName;
...
}
Then I create a map like this:
Map<Person, String> map = new HashMap<Person, String>();
map.put(new Person("Bob", "Builder"), "string1");
map.put(new Person("Bob", "NotBuilding"), "string2");
What should a valid json representation of the above look like? if it is indeed possible?
You should have a serialization/deserialization mechanism for class Person first. For example each Person may have a unique id, which can be used as the map key. Java uses its hashCode()
for serializing Person object to a key.
{ "Persons" : { "string1" : { "class" : "Person", "firstName" : "Bob", "lastName" : "Builder" }, "string2" : { "class" : "Person", "firstName" : "Bob", "lastName" : "NotBuilding" } } }
This is only from a JSON stand point and assuming you name your map "Persons". You need to find a way to serialize and de-serialize but I guess you can find one in java.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.