如何将此 Json 映射到 Gson?

How can I map this Json to Gson?

我正在尝试将此 json 映射到要与 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"
    }
]

项目运行正常,但当我尝试访问端点时出现此错误:

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)

调试项目我看到报错的那一行是代码中标有错误注释的那一行,这可能是映射?我怎样才能正确映射这个对象?

已映射 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;
}

挖矿功能:

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");
        }
    }

谁能帮助我,我将不胜感激!!

请检查一下,

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);

您设置了日期格式并且 POJO 使用注释 @JsonSerialize(using=DateSerializer.class)

我不确定我的回答是否适合您,请尝试提供帮助。

{ “高”:“4.1613”, “低”:“4.1227”, "varBid": "-0.0146", "pctChange": "-0.35", “出价”:“4.1318”, “问”:“4.1323”, “时间戳”:“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.