Hi I have a Java Class like,
// code
class A{
private int i;
private String j;
// getters/setters
}
I want to use this object as a map key in my json, how i can do this? e.g : i want something like - {javaObject:value}
Hi I have a Java Class like,
// code
class A{
private int i;
private String j;
// getters/setters
}
I want to use this object as a map key in my json, how i can do this? e.g : i want something like - {javaObject:value}
In that case when you want to convert object to json, use this link. Gson can be used to do that.
You can bind key with any object and map the key-name with json key, use Jackson library to achieve this -
1.You can create data binding POJO like below -
class A{
@JsonProperty("key_name")
private int i;
@JsonProperty("key_name")
private String j;
// getters/setters
}
2.If you want Object A to be used as a key, then create a pojo like below -
class JsonData{
@JsonProperty("key_name")
private A object;
//getter //setter
}
Hi I've find the correct answer, actually for complex object as a map key in JSON we have to use:-
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.enableComplexMapKeySerialization();
Gson mapper = gsonBuilder.create();
//Object to JSON in String
String jsonInString = mapper.toJson(myobject); //myobject=any object of class A