Hé all, I'm trying to solve an api response from: https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=2
The response consist an 2D array of the Pojo24 class (see below) I want to fetch te response of the api into the Pojo24.class (getters and setters included). So in the end I want to put it into a database. I tried to use the Gson.class, unfortunately it didnt worked out. Is there somebody who can take me along with the solution? Tnx in advance!
public class Pojo24{
private Long id;
private Long openTime;
private String open;
private String high;
private String low;
private String close;
private String volume;
private Long closeTime;
private String quoteAssesVolume;
private Long numberOfTrades;
private String tbbav; //taker buy base asset volume
private String tbqav; //taker buy quote asset volume
private String ignore;
}
package com.example.Binance24hr;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Binance24hrApplication {
public static void main(String[] args) {
SpringApplication.run(Binance24hrApplication.class, args);
String url = "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=2";
Gson gson = new Gson();
List<Object[]> result = gson.fromJson(url, new TypeToken<ArrayList<Object[]>>(){}.getType());
List<Pojo24> pojo24List = result.stream()
.map(e -> {
Pojo24 pojo24 = new Pojo24();
// pojo24.setId(new Double((Double) e[0]).longValue());
pojo24.setOpenTime(new Double((String) e[1]).longValue());
pojo24.setOpen((String) e[2]);
pojo24.setHigh((String) e[3]);
pojo24.setLow((String) e[4]);
pojo24.setClose((String) e[5]);
pojo24.setVolume(String.valueOf(e[6]));
pojo24.setCloseTime(new Double((String) e[7]).longValue());
pojo24.setQuoteAssesVolume(String.valueOf(e[8]));
pojo24.setNumberOfTrades(new Double((String) e[9]).longValue());
pojo24.setTbbav((String) e[10]);
pojo24.setTbqav((String) e[11]);
// pojo24.setIgnore((String) e[12])
return pojo24;
})
.collect(Collectors.toList());
}
}
When im running this i got the following exception:
2022-01-27 19:33:15.661 INFO 10168 --- [ main] c.e.Binance24hr.Binance24hrApplication: Started Binance24hrApplication in 7.669 seconds (JVM running for 9.848) Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:939) at com.google.gson.Gson.fromJson(Gson.java:892) at com.google.gson.Gson.fromJson(Gson.java:841) at com.example.Binance24hr.Binance24hrApplication.main(Binance24hrApplication.java:20) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.int ernal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) at com.google.gson.Gson.fromJson(Gson.java:927)... 3 more
As I commented under OP, I can only find 12 elements in nested JSON array of given API response, but there are 13 fields in class Pojo24
. So, I assumed that the last field ignore
can be ignored and you can first deserialize the response JSON string to a list of Object
array, then assign each field to your class Pojo24
as follows:
Gson gson = new Gson();
List<Object[]> result = gson.fromJson(jsonStr, new TypeToken<ArrayList<Object[]>>(){}.getType());
List<Pojo24> pojo24List = result.stream()
.map(e -> {
Pojo24 pojo24 = new Pojo24();
pojo24.setId(new Double((Double) e[0]).longValue());
pojo24.setOpenTime(new Double((String) e[1]).longValue());
pojo24.setOpen((String) e[2]);
pojo24.setHigh((String) e[3]);
pojo24.setLow((String) e[4]);
pojo24.setClose((String) e[5]);
pojo24.setVolume(String.valueOf(e[6]));
pojo24.setCloseTime(new Double((String) e[7]).longValue());
pojo24.setQuoteAssesVolume(String.valueOf(e[8]));
pojo24.setNumberOfTrades(new Double((String) e[9]).longValue());
pojo24.setTbbav((String) e[10]);
pojo24.setTbqav((String) e[11]);
// pojo24.setIgnore((String) e[12])
return pojo24;
})
.collect(Collectors.toList());
package com.example.Binance24hr;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Binance24hrApplication {
public static void main(String[] args) {
SpringApplication.run(Binance24hrApplication.class, args);
String url = "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=2";
Gson gson = new Gson();
List<Object[]> result = gson.fromJson(url, new TypeToken<ArrayList<Object[]>>(){}.getType());
List<Pojo24> pojo24List = result.stream()
.map(e -> {
Pojo24 pojo24 = new Pojo24();
// pojo24.setId(new Double((Double) e[0]).longValue());
pojo24.setOpenTime(new Double((String) e[1]).longValue());
pojo24.setOpen((String) e[2]);
pojo24.setHigh((String) e[3]);
pojo24.setLow((String) e[4]);
pojo24.setClose((String) e[5]);
pojo24.setVolume(String.valueOf(e[6]));
pojo24.setCloseTime(new Double((String) e[7]).longValue());
pojo24.setQuoteAssesVolume(String.valueOf(e[8]));
pojo24.setNumberOfTrades(new Double((String) e[9]).longValue());
pojo24.setTbbav((String) e[10]);
pojo24.setTbqav((String) e[11]);
// pojo24.setIgnore((String) e[12])
return pojo24;
})
.collect(Collectors.toList());
}
}
When im running this i got the following exception:
2022-01-27 19:33:15.661 INFO 10168 --- [ main] c.e.Binance24hr.Binance24hrApplication: Started Binance24hrApplication in 7.669 seconds (JVM running for 9.848) Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:939) at com.google.gson.Gson.fromJson(Gson.java:892) at com.google.gson.Gson.fromJson(Gson.java:841) at com.example.Binan ce24hr.Binance24hrApplication.main(Binance24hrApplication.java:20) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) at com.google.gson.Gson.fromJson(Gson.java:927)... 3 more
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.