无法反序列化'java.util.ArrayList'的实例`
我无法从JSON文件获取数据。我的JAVA端正在工作,但在PHP中进行了更改,以组装JSON,因为我不能做解析器,我不知道在PHP中知道我得到的JSON格式,但我可以访问代码。 JSON装载的页面如下所示:无法反序列化'java.util.ArrayList'的实例`,java,php,json,spring-boot,Java,Php,Json,Spring Boot,我无法从JSON文件获取数据。我的JAVA端正在工作,但在PHP中进行了更改,以组装JSON,因为我不能做解析器,我不知道在PHP中知道我得到的JSON格式,但我可以访问代码。 JSON装载的页面如下所示: <?php $startTime = microtime(true); include_once("../utils/config.php"); include_once("../utils/utils.php"); include_once("../s
<?php
$startTime = microtime(true);
include_once("../utils/config.php");
include_once("../utils/utils.php");
include_once("../services/rest.utils.php");
header("Content-Type: application/json",true);
$from = getADate('from', true);
$to = getADate('to', false);
$fromDate = '';
$toDate = '';
if ($from <> '') { $fromDate = $from; }
if ($to <> 'NULL') { $toDate = $to; }
$body = file_get_contents('php://input');
if (signatureCheck($body)) {
$cta = 0;
$database = conectaDatabase();
$script = '';
$pfx = Config::getPrefixo();
$sql = "SELECT RelatorioID, ProfessorID, AlunoID, TurmaID,
Bimestre, Data, TipoRelatorio, Conteudo, Situacao FROM {$pfx}Relatorios
WHERE Data >= $fromDate";
if ($toDate <> '') {
$sql .= " AND Data < $toDate";
}
$qry = $database->query($sql);
$lista = array();
$cta = 0;
while ($row = $qry->fetchObject()) {
$row->TipoRelatorio = utf8_encode($row->TipoRelatorio);
$row->Conteudo = utf8_encode($row->Conteudo);
$lista[] = $row;
$cta++;
}
@$output->response = new StdClass(); // Anonymous object, remove the warning
$output->response->descricao = "$cta relatórios importados";
$output->response->status = 200;
$output->relatorios = $lista;
$endTime = microtime(true);
$timeSpent = $endTime - $startTime;
$output->response->timeSpent = $timeSpent;
$saida = json_encode($output, JSON_UNESCAPED_UNICODE);
echo $saida;
desconectaDB($database);
} else {
$description = "não autorizado";
$status = "401";
$endTime = microtime(true);
$timeSpent = $endTime - $startTime;
echo "{\"status\":{$status}, \"descricao\":\"$description\", \"timeSpent\": \"{$timeSpent}\"}";
}
这就是我试图解析JSON时发生的错误
org.springframework.web.client.RestClientException: Error while extracting response for type
[java.util.List<com.test.bws.response.RelatorioResponse>] and
content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]
Error while extracting response for type [java.util.List<com.test.bws.response.RelatorioResponse>] and content type [application/json]
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]
org.springframework.web.client.RestClientException:为类型提取响应时出错
[java.util.List]和
内容类型[应用程序/json];嵌套的异常为org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法反序列化'java.util.ArrayList'的实例,因为它超出了START_对象标记;嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化'java.util.ArrayList'的实例,因为它超出了START_对象标记
在[源:(PushbackInputStream);行:1,列:1]
提取类型[java.util.List]和内容类型[application/json]的响应时出错
org.springframework.http.converter.httpmessagenoteradableexception:JSON解析错误:无法反序列化'java.util.ArrayList'的实例,该实例不在START_对象标记中;嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化'java.util.ArrayList'的实例,因为它超出了START_对象标记
在[源:(PushbackInputStream);行:1,列:1]
感谢您的帮助问题在于您的JSON如下所示:
{
"response": {
...
},
"relatorios": [
{
...
}
]
}
HttpEntity<String> entity = new HttpEntity<>("parameters", new HttpHeaders());
ResponseEntity<RestResponse> response = new RestTemplate().exchange(targetUrl, HttpMethod.GET, entity, RestResponse.class);
List<RelatorioResponse> responses = response.getBody().getRelatorios();
您正试图将其直接映射到relatorios
列表。您应该创建另一个response
对象。这应该包含整个JSON响应内容:
public class RestResponse {
private MyResponse response;
private List<RelatorioResponse> relatorios;
// getter and setter
}
然后,您的请求代码应该如下所示:
{
"response": {
...
},
"relatorios": [
{
...
}
]
}
HttpEntity<String> entity = new HttpEntity<>("parameters", new HttpHeaders());
ResponseEntity<RestResponse> response = new RestTemplate().exchange(targetUrl, HttpMethod.GET, entity, RestResponse.class);
List<RelatorioResponse> responses = response.getBody().getRelatorios();
HttpEntity entity=new-HttpEntity(“参数”,new-HttpHeaders());
ResponseEntity response=new RestTemplate().exchange(targetUrl,HttpMethod.GET,entity,RestResponse.class);
列出响应=response.getBody().getRelatorios();
JSON中的响应是什么样子的?要反序列化数组列表,我希望您需要一个对象的JSON数组。至少,在else
子句中返回的JSON看起来不符合此标准。@akokskis我插入了他们发送的测试的图像
HttpEntity<String> entity = new HttpEntity<>("parameters", new HttpHeaders());
ResponseEntity<RestResponse> response = new RestTemplate().exchange(targetUrl, HttpMethod.GET, entity, RestResponse.class);
List<RelatorioResponse> responses = response.getBody().getRelatorios();