I have a JSON (stored in a string, file etc) and I want to read it and create the
java instance.
For example for this class
@Data
public class Rate {
private String currency;
private BigDecimal buyRate;
private BigDecimal midRate;
private BigDecimal sellRate;
private String conversionType;
}
I have this buffer in a String
{
"currency": "BRL",
"buyRate": 4.18390000,
"midRate": 4.18390000,
"sellRate": 4.18390000,
"conversionType": "D"
}
how can I create the object instance from the string buffer?
3
Answers
If you’re using Spring Boot, you have Jackson lib included by default, so you may read your object like this:
Maybe you could try something like that:
import com.google.gson.Gson;
Rate rate = new Gson().fromJson(json, Rate.class);
Where
Rate
is your class, andjson
is your json string.As others have suggested, use Jackson: