亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法反序列化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 關注
  • 350 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號