为了账号安全,请及时绑定邮箱和手机立即绑定

无法反序列化java.util.ArrayList的实例

无法反序列化java.util.ArrayList的实例

PHP
森林海 2021-05-13 10:19:59
我在尝试从JSON文件获取数据时遇到了麻烦。我的JAVA端正在工作,但是在组装JSON的PHP中进行了更改,因为我无法执行解析器,因此我不知道在PHP中知道我正在获取的JSON格式,但是我可以访问代码。JSON挂载的页面是这样的:<?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);    }
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

问题是您的JSON如下所示:


{

  "response": {

    ...

  },

  "relatorios": [

    {

      ...

    }

  ]

}

您正在尝试将其直接映射到relatorios列表。您应该RestResponse改为创建另一个对象。这应该包含整个JSON响应内容:


public class RestResponse {

    private MyResponse response;

    private List<RelatorioResponse> relatorios;

    // getter and setter

}

该response对象应代表responseJSON中的部分。如果不需要,也可以添加@JsonIgnoreProperties(ignoreUnknown = true)到RestResponse类中并忽略response属性:


@JsonIgnoreProperties(ignoreUnknown = true)

public static class RestResponse {

    private List<RelatorioResponse> relatorios;

    // getter and setter

}

您的请求代码应如下所示:


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();


查看完整回答
反对 回复 2021-05-28
  • 1 回答
  • 0 关注
  • 329 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信