Home > OS >  How can I map this Json to Gson?
How can I map this Json to Gson?

Time:09-28

I'm trying to map this json to an object to use with gson:

[
    {
        "code": "USD",
        "codein": "BRL",
        "name": "Dólar Americano/Real Brasileiro",
        "high": "4.133",
        "low": "4.133",
        "varBid": "0.0004",
        "pctChange": "0.01",
        "bid": "4.1326",
        "ask": "4.1333",
        "timestamp": "1579036500",
        "create_date": "2020-01-14 18:15:00"
    },
    {
        "high": "4.1613",
        "low": "4.1227",
        "varBid": "-0.0124",
        "pctChange": "-0.3",
        "bid": "4.134",
        "ask": "4.1345",
        "timestamp": "1579036447"
    },
    {
        "high": "4.1613",
        "low": "4.1227",
        "varBid": "-0.0146",
        "pctChange": "-0.35",
        "bid": "4.1318",
        "ask": "4.1323",
        "timestamp": "1579036270"
    },
    {
        "high": "4.1613",
        "low": "4.1227",
        "varBid": "-0.0137",
        "pctChange": "-0.33",
        "bid": "4.1326",
        "ask": "4.1333",
        "timestamp": "1579036059"
    },
    {
        "high": "4.1613",
        "low": "4.1227",
        "varBid": "-0.0139",
        "pctChange": "-0.33",
        "bid": "4.1325",
        "ask": "4.133",
        "timestamp": "1579035872"
    }
]

The project works normally, but when I try to access the endpoint I have this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
    at com.google.gson.Gson.fromJson(Gson.java:932)
    at com.google.gson.Gson.fromJson(Gson.java:897)
    at com.google.gson.Gson.fromJson(Gson.java:846)
    at com.google.gson.Gson.fromJson(Gson.java:817)
    at br.com.leomanzini.dollar.price.service.DollarPriceService.getHistoryDollarPrice(DollarPriceService.java:66)
    at br.com.leomanzini.dollar.price.controller.DollarPriceController.getHistoryDollarPrice(DollarPriceController.java:28)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:384)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)

Debugging the project I could see that the line giving the error is the line marked in the code with the error comment, this could be the mapping? How could I map this object correctly?

Mapped class:

package br.com.leomanzini.dollar.price.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class HistoryDollarResponse {
    
    private String pctChange;
    private String bid;
    private String timestamp;
}

Mine function:

public HistoryDollarResponse getHistoryDollarPrice(String day, String month, String year) throws Exception {
        
        try {
            PropertiesLoader.load();
            
            String date = year   month   day;
            
            String serviceUrl = PropertiesLoader.getHistoryUrl();
            serviceUrl = String.format(serviceUrl, date);
            
            URL url = new URL(serviceUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            if (connection.getResponseCode() != 200) {
                throw new RuntimeException("HTTP error code : "   connection.getResponseCode());
            }

            BufferedReader response = new BufferedReader(new InputStreamReader((connection.getInputStream())));
            String jsonToString = Convert.jsonIntoString(response);

            Gson gson = new Gson();
            HistoryDollarResponse returnObject = gson.fromJson(jsonToString, HistoryDollarResponse.class); //error

            return returnObject;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Can't access the webpage");
        }
    }

Someone who could help me I'll be thankful!!

CodePudding user response:

Please check this out,

String DB_JSON_FORMATE_1 = "yyyy-MM-dd'T'HH:mm:ss.SSS";

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat(DB_JSON_FORMATE_1);
Gson gson = gsonBuilder.create();
Type listType = new TypeToken<List<HistoryDollarResponse>>() {}.getType();
List<HistoryDollarResponse> historyDollarResponse = gson.fromJson(#PASS_YOU_STING#, listType);

You set your date format and POJO use annotation @JsonSerialize(using=DateSerializer.class)

CodePudding user response:

I am not sure my answer is good enough for you, just try to help.

{ "high": "4.1613", "low": "4.1227", "varBid": "-0.0146", "pctChange": "-0.35", "bid": "4.1318", "ask": "4.1323", "timestamp": "1579036270" }

public class Staff {

private String high;
private String low;
private String varBid;              
private String pctChange;            
private String bid; 
private String ask; 
private String timestamp; 
}

Gson gson = new Gson();

//  JSON string to Java object
// this is example like your json 
//String json = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(json, Staff.class);
    
in your json syntax, it is about timestamp, if you need or want, you should be convert them to int, double, float and timestramp
for example Timestamp.valueOf("2018-11-12 01:02:03.123456789") you can search out lots about string to int, float or double.
  • Related