Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:09:03+00:00 2026-05-23T16:09:03+00:00

I’m trying to process some JSON into a Java object using some beans and

  • 0

I’m trying to process some JSON into a Java object using some beans and GSON. However, the keys in the JSON code I am using can change over time, based on what is most recently traded. I can manually make a bean file that includes each currency code, but in the end, they can all be different, and my program broken.

Here is my code:

From my main class:

public void updateData() {
    Data data;
    String s = null;
    try {
        s = DataGetter.getJSON("http://bitcoincharts.com/t/weighted_prices.json");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    s = s.replaceAll("7d", "week");
    s = s.replaceAll("30d", "month");
    s = s.replaceAll("24h", "day");
    s = s.toLowerCase();
    data = new Gson().fromJson(s, Data.class);
}

Data.java:

package tehsusenoh.bittick.get;

import java.util.ArrayList;
import java.util.List;

public class Data {

public enum Currency {
    USD, AUD, RUB, GAU, BGN, CNY, SLL, INR, GBP, PLN, SAR, EUR, CLP, CAD
}

private CurrencyData usd;
private CurrencyData aud;
private CurrencyData rub;
private CurrencyData gau;
private CurrencyData bgn;
private CurrencyData cny;
private CurrencyData sll;
private CurrencyData inr;
private CurrencyData gbp;
private CurrencyData pln;
private CurrencyData sar;
private CurrencyData eur;
private CurrencyData clp;
private CurrencyData cad;

public List<CurrencyData> getData() {
    List<CurrencyData> d = new ArrayList<CurrencyData> ();
    d.add(usd);
    d.add(aud);
    d.add(rub);
    d.add(gau);
    d.add(bgn);
    d.add(cny);
    d.add(sll);
    d.add(inr);
    d.add(gbp);
    d.add(pln);
    d.add(sar);
    d.add(eur);
    d.add(clp);
    d.add(cad);
    return d;
}

public CurrencyData get(Currency c) {
    switch (c) {
    case USD: {
        return usd;
    }
    case AUD: {
        return aud;
    }
    case RUB: {
        return rub;
    }
    case GAU: {
        return gau;
    }
    case BGN: {
        return bgn;
    }
    case CNY: {
        return sll;
    }
    case INR: {
        return inr;
    }
    case GBP: {
        return gbp;
    }
    case PLN: {
        return pln;
    }
    case SAR: {
        return sar;
    }
    case EUR: {
        return eur;
    }
    case CLP: {
        return clp;
    }
    case CAD: {
        return cad;
    }
    default: {
        return null;
    }
    }
}

public void addCodes() {
    usd.setCode("USD");
    aud.setCode("AUD");
    rub.setCode("RUB");
    gau.setCode("GAU");
    bgn.setCode("BGN");
    cny.setCode("CNY");
    sll.setCode("SLL");
    inr.setCode("INR");
    gbp.setCode("GBP");
    pln.setCode("PLN");
    sar.setCode("SAR");
    eur.setCode("EUR");
    clp.setCode("CLP");
    cad.setCode("CAD");

}

public String toString() {
    return "" + getData();
}
}

And finally, CurrencyData.java:

package tehsusenoh.bittick.get;

public class CurrencyData {

private Double week;
private Double month;
private Double day;
private String code;

public CurrencyData() {}

public Double getWeek() { return week; }
public Double getMonth() { return month; }
public Double getDay() { return day; }
public String getCode() { return code; }

public void setWeek(Double d) { week = d; }
public void setMonth(Double d) { month = d; }
public void setDay(Double d) { day = d; }
public void setCode(String aCode) { code = aCode; }

public String toString() {
    return code + ":: week:" + week + " month:" + month + " day:" + day;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:09:04+00:00Added an answer on May 23, 2026 at 4:09 pm

    Since the nice person that generated the JSON chose to use a JSON object to hold a collection of things, instead of using a JSON array, Jim’s suggestion to use a Java Map is a natural fit.

    With Gson, here’s what that solution would look like.

    import java.io.FileReader;
    import java.lang.reflect.Type;
    import java.math.BigDecimal;
    import java.util.Map;
    
    import com.google.gson.Gson;
    import com.google.gson.annotations.SerializedName;
    import com.google.gson.reflect.TypeToken;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
        Type currencyMapType = new TypeToken<Map<String, Currency>>() {}.getType();
        Map<String, Currency> currencyMap = gson.fromJson(new FileReader("input.json"), currencyMapType);
        System.out.println(currencyMap);
      }
    }
    
    class Currency
    {
      @SerializedName("7d")
      BigDecimal _7d;
      @SerializedName("30d")
      BigDecimal _30d;
      @SerializedName("24h")
      BigDecimal _24h;
    
      @Override
      public String toString()
      {
        return String.format("{7d:%s, 30d:%s, 24h:%s}", _7d, _30d, _24h);
      }
    }
    

    The following update generates a list of currencies from the currency data in the deserialized map.

    import java.io.FileReader;
    import java.lang.reflect.Type;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import com.google.gson.Gson;
    import com.google.gson.annotations.SerializedName;
    import com.google.gson.reflect.TypeToken;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
        Type currencyDataMapType = new TypeToken<Map<String, CurrencyData>>() {}.getType();
        Map<String, CurrencyData> currencyDataMap = gson.fromJson(new FileReader("input.json"), currencyDataMapType);
        List<Currency> currencies = fromDataMap(currencyDataMap);
        System.out.println(currencies);
      }
    
      static List<Currency> fromDataMap(Map<String, CurrencyData> currencyDataMap)
      {
        List<Currency> currencies = new ArrayList<Currency>(currencyDataMap.size());
        for (Entry<String, CurrencyData> entry : currencyDataMap.entrySet())
        {
          String code = entry.getKey();
          currencies.add(new Currency(code, currencyDataMap.get(code)));
        }
        return currencies;
      }
    }
    
    class Currency
    {
      String code;
      CurrencyData data;
    
      Currency(String code, CurrencyData data)
      {
        this.code = code;
        this.data = data;
      }
    
      @Override
      public String toString()
      {
        return String.format("{code:%s, 7d:%s, 30d:%s, 24h:%s}", code, data._7d, data._30d, data._24h);
      }
    }
    
    class CurrencyData
    {
      @SerializedName("7d")
      BigDecimal _7d;
      @SerializedName("30d")
      BigDecimal _30d;
      @SerializedName("24h")
      BigDecimal _24h;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.