skip to Main Content

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


  1. If you’re using Spring Boot, you have Jackson lib included by default, so you may read your object like this:

    import com.fasterxml.jackson.databind.ObjectMapper;
    
    // ...
    
    String json = "{ "currency": "BRL", "buyRate": 4.18390000, "midRate": 4.18390000, "sellRate": 4.18390000, "conversionType": "D" }";
    
    ObjectMapper mapper = new ObjectMapper();
    
    Rate rate = null;
    try {
        rate = mapper.readValue(json, Rate.class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    // ...
    
    Login or Signup to reply.
  2. 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, and json is your json string.

    Login or Signup to reply.
  3. As others have suggested, use Jackson:

    import java.io.IOException;
    import java.math.BigDecimal;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.Data;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class JacksonExample {
        private static final Logger log = LoggerFactory.getLogger(JacksonExample.class);
        private static ObjectMapper mapper = new ObjectMapper();
        private static String jsonData = "{ "currency": "BRL", "buyRate": 4.18390000, "midRate": 4.18390000, "sellRate": 4.18390000, "conversionType": "D" }";
    
        public static void main(String[] args) {
            Rate rate = parseRateData(jsonData);
            System.out.println(rate); // JacksonExample.Rate(currency=BRL, conversionType=D, buyRate=4.18390000, midRate=4.18390000, sellRate=4.18390000)
        }
    
        public static Rate parseRateData(String rateData) {
            return parseJsonData(rateData, Rate.class);
        }
    
        public static <E> E parseJsonData(String jsonData, Class<E> clazz) {
            try {
                return mapper.readValue(jsonData, clazz);
            } catch (IOException e) {
                log.error(String.format("Could not parse: %s", jsonData), e);
            }
            return null;
        }
    
        @Data
        public static class Rate {
            private String currency, conversionType;
            private BigDecimal buyRate, midRate, sellRate;
        }
    }
    
    <dependencies>
      <!-- DTOs -->
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <scope>provided</scope>
      </dependency>
      <!-- Deserialization -->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
      </dependency>
      <!-- Logging -->
      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
      </dependency>
    </dependencies>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search