gson 尝试使用类来包含JSON响应

8dtrkrch  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在尝试创建一个java类,它将包含来自JSON的响应,如下所示:

{
    "Global Quote": {
        "01. symbol": "IBM",
        "02. open": "160.0000",
        "03. high": "162.0400",
        "04. low": "160.0000",
        "05. price": "161.9600",
        "06. volume": "4561342",
        "07. latest trading day": "2023-12-08",
        "08. previous close": "160.2200",
        "09. change": "1.7400",
        "10. change percent": "1.0860%"
    }
}

字符串
到目前为止,我已经尝试使用了两个独立的类,一个有很多String和double对象,我最近使用的是:
第一个月
我一直在使用谷歌的GSON.fromJSON将响应解析为VantageResponse类的对象,但它总是返回null。我做错了什么?

wj8zmpe1

wj8zmpe11#

看起来你的思路是正确的,但是你的VantageResponse类的结构可能有问题。下面是一个修改后的版本,应该可以工作:

import com.google.gson.annotations.SerializedName;

import java.util.Map;

public class VantageResponse {
    @SerializedName("Global Quote")
    private GlobalQuote globalQuote;

    public GlobalQuote getGlobalQuote() {
        return globalQuote;
    }

    public void setGlobalQuote(GlobalQuote globalQuote) {
        this.globalQuote = globalQuote;
    }

    public static class GlobalQuote {

        @SerializedName("01. symbol")
        private String symbol;
        @SerializedName("02. open")
        private double open;
        @SerializedName("03. high")
        private double high;
        @SerializedName("04. low")
        private double low;
        @SerializedName("05. price")
        private double price;
        @SerializedName("06. volume")
        private long volume;
        @SerializedName("07. latest trading day")
        private String latestTradingDay;
        @SerializedName("08. previous close")
        private double previousClose;
        @SerializedName("09. change")
        private double change;
        @SerializedName("10. change percent")
        private String changePercent;

        // Getters and setters for all the fields

        // You can generate these using your IDE or write them manually
        // For simplicity, I'm showing just one getter here

        public String getSymbol() {
            return symbol;
        }

        public double getOpen() {
            return open;
        }

        public void setSymbol(String symbol) {
            this.symbol = symbol;
        }
    }
}

字符串
有了这个结构,你可以使用Gson来格式化JSON响应:

import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        String jsonResponse = "{ \"Global Quote\": { \"01. symbol\": \"IBM\", \"02. open\": \"160.0000\", \"03. high\": \"162.0400\", \"04. low\": \"160.0000\", \"05. price\": \"161.9600\", \"06. volume\": \"4561342\", \"07. latest trading day\": \"2023-12-08\", \"08. previous close\": \"160.2200\", \"09. change\": \"1.7400\", \"10. change percent\": \"1.0860%\" } }";

        Gson gson = new Gson();
        VantageResponse vantageResponse = gson.fromJson(jsonResponse, VantageResponse.class);

        if (vantageResponse != null && vantageResponse.getGlobalQuote() != null) {
            System.out.println("Symbol: " + vantageResponse.getGlobalQuote().getSymbol());
            System.out.println("Open: " + vantageResponse.getGlobalQuote().getOpen());
            // Add more fields as needed
        } else {
            System.out.println("Failed to parse JSON response");
        }
    }
}

相关问题