I try to parse value from JSON:
{"BTC_BCN":{"id":7,"last":"0.00000086","lowestAsk":"0.00000086","highestBid":"0.00000085","percentChange":"0.14666666","baseVolume":"368.86654762","quoteVolume":"498378738.00151879","isFrozen":"0","high24hr":"0.00000086","low24hr":"0.00000068"},"BTC_ETH":{"id":8,"last":"0.00003800","lowestAsk":"0.00003815","highestBid":"0.00003800","percentChange":"0.12326337","baseVolume":"34.17037464","quoteVolume":"955538.55551651","isFrozen":"0","high24hr":"0.00003883","low24hr":"0.00003221"}}
the values which I am interested in is "id":8,"last":"0.00003800"
.
I have beans with RestTemplate:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
and the one which ask website by API every 5 seconds
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
while(true)
{
BTC_Poloniex btc_poloniex = restTemplate.getForObject(
"https://poloniex.com/public?command=returnTicker", BTC_Poloniex.class);
log.info(btc_poloniex.toString());
TimeUnit.SECONDS.sleep(5);
}
};
}
and class to read the values:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class BTC_Poloniex {
private BTC_BCN BTC_BCN;
public BTC_Poloniex() {}
public BTC_BCN getBTC_BCN() {
return BTC_BCN;
}
public void setBTC_BCN(BTC_BCN btc_bcn)
{
this.BTC_BCN = btc_bcn;
}
@Override
public String toString() {
return "BTC_Poloniex{" +
"BTC_BCN=" + BTC_BCN +
'}';
}
}
and class (which I think would be better if it were inner class)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class BTC_BCN {
private long id;
private long last;
private long lowestAsk;
public BTC_BCN(){}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getLast() {
return last;
}
public void setLast(long last) {
this.last = last;
}
public long getLowestAsk() {
return lowestAsk;
}
public void setLowestAsk(long lowestAsk) {
this.lowestAsk = lowestAsk;
}
public String toString()
{
return "BTC_BCN{" +
" id='" + id + '\'' +
" last'= " + last + '\'' +
" lowestAsk'= " + lowestAsk + '\'' +
'}';
}
}
when I try to get these values I always have null
2018-01-12 14:00:14.829 INFO 18017 --- [ main] com.example.demo.BitbayApplication : BTC_ETH{BTC_ETH=null}
why is that so? I can easily read simplier JSON but have problem with this one.
Simple solution is to use Maps. Sample is below.
Using this code you have access to every field from json :)
I hope that my solution is good enough for you :)
@Service
public class RestTemplateService {
@Autowired
RestTemplate restTemplate;
public void getInfoFromPoloniex() {
HashMap<String,Map> answer = restTemplate.getForObject("https://poloniex.com/public?command=returnTicker",
new HashMap<>().getClass());
answer.values().forEach(a -> System.out.println(a.toString()));
System.out.println(answer.get("BTC_GAME").getClass().toString());
answer.get("BTC_GAME").keySet().stream().forEach(k -> System.out.println(k.toString()));
}
}