我正在尝试制作在线Cantor。我需要从外部API确切地从该页面获取具有货币值的表:http://api.nbp.pl/api/exchangerates/tables/A?format=json 我想在货币课上得到答案。有人可以帮我完成这项任务吗?
@Service
public class CurrentFromNBPImpl implements CurrentFromNBP {
@Override
public Currency getValueOfCurrency(String currencyCode) throws WrongCurrencyCode {
Currency currency = null;
try {
URL url = new URL("http://api.nbp.pl/api/exchangerates/tables/A?format=json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
if (connection.getResponseCode() == 404)
throw new WrongCurrencyCode(currencyCode);
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String jsonOutput = bufferedReader.readLine();
connection.disconnect();
ObjectMapper objectMapper = new ObjectMapper();
Currency list = objectMapper.readValue(jsonOutput, Currency.class);
System.out.println(list);
} catch (IOException e) {
e.printStackTrace();
}
assert false;
return currency;
}
}
@Data
public class Currency {
@JsonProperty("table")
private String table;
@JsonProperty("no")
private String no;
@JsonProperty("effectiveDate")
private String effectiveDate;
@JsonProperty("rates")
private List<Rate> rates = null;
}
@Data
public class Rate {
@JsonProperty("currency")
private String currency;
@JsonProperty("code")
private String code;
@JsonProperty("mid")
private Double mid;
}
日志:
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化com.kolej.bartosz.challenge.domain.Currency
的实例
在[来源:(String)“ [{” table“:” A“,” no“:” 062 / A / NBP / 2019“,”有效日期“:” 2019-03-28“,”费率“:[{ “ currency”:“ bat(Tajlandia)”,“ code”:“ THB”,“ mid”:0.1202},{“ currency”:“ dolaramerykański”,“ code”:“ USD”,“ mid”:3.8202} ,{“ currency”:“ dolar australijski”,“ code”:“ AUD”,“ mid”:2.7098},{“ currency”:“ dolar Hongkongu”,“ code”:“ HKD”,“ mid”:0.4867} ,{“ currency”:“ dolar kanadyjski”,“ code”:“ CAD”,“ mid”:2.8461},{“ currency”:“ dolar nowozelandzki”,“ code”:“ NZD”,“ mid”:2.6006} ,{“ currency”:“ dolar singapurski”,“ code”:“ SGD”,“ mid”:2.8179},{“ currency”:“ eu” [被截断的1616个字符];行:1,列:1] >
答案 0 :(得分:1)
您要反序列化的json对象是一个jsonArray。您需要反序列化为extern int obtain_order(); /* See parser.y for description */
int main(void)
{
char ***argvv;
//int command_counter;
int num_commands;
//int args_counter;
char *filev[3];
int bg;
int ret;
setenv("Acc","0",1);
setbuf(stdout, NULL); /* Unbuffered */
setbuf(stdin, NULL);
while (1)
{
fprintf(stderr, "%s", "msh> "); /* Prompt */
ret = obtain_order(&argvv, filev, &bg);
if (ret == 0) break; /* EOF */
if (ret == -1) continue; /* Syntax error */
num_commands = ret - 1; /* Line */
if (num_commands == 0) continue; /* Empty line */
if(num_commands==1){
//Implementación propia para salir del mini-shell
if(strcmp(argvv[0][0], "exit") == 0){
exit(0);
}
if(strcmp(argvv[0][0],"myTime")==0){
//HERE IS WHERE I HAVE TO IMPLEMENT THE INTERN COMMAND MYTIME
}else{ //For all other commands ls, etc... (with pipes (max 3)and background)
int pid;
int estado;
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del mandato simple\n");
return (-1);
case 0: /* hijo */
if (filev[0] != NULL) {
close(STDIN_FILENO);
open(filev[0],O_RDONLY);
}
if (filev[1] != NULL) {
close(STDOUT_FILENO);
open(filev[1],O_CREAT|O_WRONLY,0666);
}
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[0][0], argvv[0]);
fprintf(stderr, "%s","Error en el execvp del mandato simple\n");
return(-1);
default: /* padre */
if(!bg){
while (wait(&estado) != pid);
}else printf("%d\n",pid);
} //fin switch (1 mandato)
} //fin if (si es comando interno o no)
}else if(num_commands==2){
int pid;
int estado;
int fd[2];
pipe(fd);
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del primer mandato\n");
return (-1);
case 0: /* hijo1 */
close(STDOUT_FILENO);
dup(fd[1]);
close(fd[0]);
close(fd[1]);
if (filev[0] != NULL) {
close(STDIN_FILENO);
open(filev[0],O_RDONLY);
}
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[0][0], argvv[0]);
fprintf(stderr, "%s","Error en el execvp del primer mandato\n");
return(-1);
default: /* padre */
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del segundo mandato\n");
return (-1);
case 0: /* hijo 2*/
close(STDIN_FILENO);
dup(fd[0]);
close(fd[0]);
close(fd[1]);
if (filev[1] != NULL) {
close(STDOUT_FILENO);
open(filev[1],O_CREAT|O_WRONLY,0666);
}
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[1][0], argvv[1]);
fprintf(stderr, "%s","Error en el execvp del segundo mandato\n");
return(-1);
default: /* padre */
close(fd[0]);
close(fd[1]);
if(!bg){
while (wait(&estado) != pid);
}else printf("%d\n",pid);
} //fin switch2 (2 mandatos)
} //fin switch1 (2 mandatso)
}else if(num_commands==3){
int pid;
int estado;
int fd[2], fd2[2];
pipe(fd);
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del primer mandato\n");
return (-1);
case 0: /* hijo1 */
close(STDOUT_FILENO);
dup(fd[1]);
close(fd[0]);
close(fd[1]);
if (filev[0] != NULL) {
close(STDIN_FILENO);
open(filev[0],O_RDONLY);
}
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[0][0], argvv[0]);
fprintf(stderr, "%s","Error en el execvp del primer mandato\n");
return(-1);
default: /* padre */
pipe(fd2);
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del segundo mandato\n");
return (-1);
case 0: /* hijo 2*/
close(STDIN_FILENO);
dup(fd[0]);
close(STDOUT_FILENO);
dup(fd2[1]);
close(fd[0]);
close(fd[1]);
close(fd2[0]);
close(fd2[1]);
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[1][0], argvv[1]);
fprintf(stderr, "%s","Error en el execvp del segundo mandato\n");
return(-1);
default: /* padre */
close(fd[0]);
close(fd[1]);
pid = fork();
switch(pid) {
case -1: /* error */
fprintf(stderr, "%s","Error en el fork del tercer mandato\n");
return (-1);
case 0: /* hijo 3*/
close(STDIN_FILENO);
dup(fd2[0]);
close(fd2[0]);
close(fd2[1]);
if (filev[1] != NULL) {
close(STDOUT_FILENO);
open(filev[1],O_CREAT|O_WRONLY,0666);
}
if (filev[2] != NULL) {
close(STDERR_FILENO);
open(filev[2],O_CREAT|O_WRONLY,0666);
}
execvp(argvv[2][0], argvv[2]);
fprintf(stderr, "%s","Error en el execvp del tercer mandato\n");
return(-1);
default: /* padre */
close(fd2[0]);
close(fd2[1]);
if(!bg){
while (wait(&estado) != pid);
}else printf("%d\n",pid);
} //fin switch 3 (3 mandatos)
} //fin switch2 (3 mandatos)
} //fin switch1 (tres mandatos)
} //fin if (número de mandatos)
} //fin while
return 0;
} //fin main
列表,而不是Currency
。
答案 1 :(得分:0)
您可以尝试
ArrayList<Currency> list = new ArrayList<>();
list = objectMapper.readValue(data, new TypeReference<ArrayList<Currency>>() {});