Laravel:如何获取名称以“-”分隔的JSON对象?

时间:2019-03-11 13:21:06

标签: arrays json laravel

如果{{ $cSiacon->'NOME-CLIENTE' }}破折号怎么办?我尝试过['NOME-CLIENTE'],但Laravel给了我:

ErrorException (E_ERROR)
Cannot use object of type Illuminate\Http\JsonResponse as array

在我的代码中:

$responseClienteSiacon = $clienteSiaconSOAP->ROS_ValidarCliente($paramClienteSiacon);
$cSiacon = response()->json(($responseClienteSiacon->ValidarCliente->Cliente));

return $cSiacon;

结果是:

[
{
"CODIGO-TIPO-PESSOA": "J",
"CPF-CNPJ": "00635344000177",
"CODIGO-GRUPO": "07384",
"NUMERO-SEQUENCIA": 0,
"NUMERO-COTA": 853,
"NOME-CLIENTE": "AUTO ESCOLA GUILHERMITTI E L LTDA",
"NUMERO-CONTRATO": 859866,
"DESCRICAO-BEM": "HONDA NXR 160 BROS",
"VALOR-BEM": 12975,
"NUMERO-TELEFONE": "017 32581859",
"DATA-PROXIMA-REUNIAO": "20190322",
"SITUACAO-COBRANCA": "N",
"DESCRICAO-SITUACAO-COBRANCA": "Normal",
"FASE-SITUACAO-COBRANCA": "N000",
"CODIGO-PLANO-COTA": 31,
"DATA-ENTREGA": "20180507",
"DATA-CONTEMPLACAO": "20170622",
"PERC-TOTAL-PAGO": 87.7196,
"PERC-TOTAL-PENDENTE": 3.1401,
"PERC-QUITACAO": 12.2804,
"CODIGO-FORMA-PAGAMENTO": 1,
"DATA-NASCIMENTO": "",
"DATA-CANCELAMENTO": "",
"CADASTRO-ATUALIZADO": "N",
"SEGMENTO-CADOC": 4,
"CEP": 15115000
},
{...
}
]

我也尝试过获得更高的位置,例如:json(($ responseClienteSiacon-> ValidarCliente));但问题依旧:破折号分隔的属性名称。 我应该先转换每个名称吗?

1 个答案:

答案 0 :(得分:2)

$object->{'NOME-CLIENTE'};

POC

https://3v4l.org/cTO7o

使用刀片

的示例
@php
    $json = <<<JSON
    [
        {
            "CODIGO-TIPO-PESSOA": "J",
            "CPF-CNPJ": "00635344000177",
            "CODIGO-GRUPO": "07384",
            "NUMERO-SEQUENCIA": 0,
            "NUMERO-COTA": 853,
            "NOME-CLIENTE": "AUTO ESCOLA GUILHERMITTI E L LTDA",
            "NUMERO-CONTRATO": 859866,
            "DESCRICAO-BEM": "HONDA NXR 160 BROS",
            "VALOR-BEM": 12975,
            "NUMERO-TELEFONE": "017 32581859",
            "DATA-PROXIMA-REUNIAO": "20190322",
            "SITUACAO-COBRANCA": "N",
            "DESCRICAO-SITUACAO-COBRANCA": "Normal",
            "FASE-SITUACAO-COBRANCA": "N000",
            "CODIGO-PLANO-COTA": 31,
            "DATA-ENTREGA": "20180507",
            "DATA-CONTEMPLACAO": "20170622",
            "PERC-TOTAL-PAGO": 87.7196,
            "PERC-TOTAL-PENDENTE": 3.1401,
            "PERC-QUITACAO": 12.2804,
            "CODIGO-FORMA-PAGAMENTO": 1,
            "DATA-NASCIMENTO": "",
            "DATA-CANCELAMENTO": "",
            "CADASTRO-ATUALIZADO": "N",
            "SEGMENTO-CADOC": 4,
            "CEP": 15115000
        }
    ]
JSON;

    $json = json_decode($json);

@endphp

    {{-- will echo "AUTO ESCOLA GUILHERMITTI E L LTDA" --}}

    {{ $json[0]->{'NOME-CLIENTE'} }}