skip to Main Content

I’m trying to make a java class that will contain a response from a JSON that looks like this:

{
    "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%"
    }
}

So far i’ve tried using two seperate classes, one with a lot of String and double objects, and I landed on this most recently:

class VantageResponse { public Map<String, Object> globalQuote; }

I’ve been using google’s GSON.fromJson to parse the response into an object of class VantageResponse but it has always returned null. What am I doing wrong?

2

Answers


  1. protected convertToMap(JSONObject response){
      JSONObject contentsOfResponse = response.getJSONObject("Global Quote");
      // you will obtain the contents of the global quote
       Map<String,Object> globalQuote = new HashMap<>();
        Iterator<String> keys = contentsOfResponse.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            Object value = contentsOfResponse.get(key);
    
            // Put the key-value pair into the HashMap
            globalQuote.put(key, value);
        }
      //you can also store this as a global variable using
      //this.globalQuote;  (in a class)
      return globalQuote;
      }
    

    Start writing a class that has a global variable of hashmap and then call this method to where you are containing the response. So far I have understood this should work. You can comment on this answer for other answer.

    Login or Signup to reply.
  2. It looks like you are on the right track, but there might be an issue with the structure of your VantageResponse class. Here’s a modified version that should work:

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

    With this structure, you can use Gson to deserialize the JSON response:

    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");
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search