-3

Basically I'd like to store the value market_price_usd from the json into my variable marketPriceUsd in the class Price, but when I try to print it out I only get a null or 0.0 output, it only works with some of the variables stored.

        BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                in.close();

        System.out.println(content);

        Gson gson = new Gson();  

        price riv = gson.fromJson(content.toString(), price.class);  
        System.out.println(content);
        System.out.println(riv.marketPriceUsd);





package test;


public class price {

        public double timestamp;
        public double marketPriceUsd;
        public double hashRate;
        public long totalFeesBtc;
        public long nBtcMined;
        public long nTx;
        public long nBlocksMined;
        public double minutesBetweenBlocks;
        public long totalbc;
        public long nBlocksTotal;
        public double estimatedTransactionVolumeUsd;
        public long blocksSize;
        public double minersRevenueUsd;
        public long nextretarget;
        public long difficulty;
        public long estimatedBtcSent;
        public long minersRevenueBtc;
        public long totalBtcSent;
        public double tradeVolumeBtc;
        public double tradeVolumeUsd;

}
Spad223
  • 1
  • 1
  • Please update your question with sample json – Smile May 13 '20 at 11:05
  • Please read [mcve] and enhance your question accordingly. Also apply Java naming conventions consistently. Class names should go UpperCase. So it should be Price, not price. And just for the record: the myriads of attributes in your class ... that really sounds much more complicated than what people mean when using the word "price". – GhostCat May 13 '20 at 11:24

1 Answers1

0

@SerializedName annotation indicates the annotated member should be serialized to JSON with the provided name value as its field name.

  • value – the desired name of the field when it is serialized or deserialized.
  • alternate – the alternative names of the field when it is deserialized. It provides more possible names other than ‘value’ attribute. If there are multiple fields that match one property, Gson will use the one that is processed last.
        @SerializedName(value = "market_price_usd", alternate = "market_price_usd")
        public double marketPriceUsd;

Same mapping annotation can be used for other JSON to Java mappings. The default value of all double fields on a object is 0.0

QuickSilver
  • 3,915
  • 2
  • 13
  • 29
  • @GhostCat Question clearly dstats a JSON field market_price_usd need to be mapped with the Java bean's field `marketPriceUsd`. The answer is not a GUESS. – QuickSilver May 13 '20 at 11:49
  • Agreed, I missed part of the question. Still it is strange ... without access to the full json, it is weird that some of the java names work. I still think the OP should enhance the question quite a bit. – GhostCat May 13 '20 at 12:13