简体   繁体   English

如何获得API响应

[英]How to get an API response

I have this response: 我有这个回应:

{
    "Contratos": [
        {
            "IdCUPS": 0,
            "CodigoCUPS": "",
            "IdContrato": 0,
            "CodigoContrato": null,
            "IdCliente": 0,
            "IdDocumento": null,
            "IdEmpresa": null,
            "Incidencias": [
                {
                    "Propiedad": "CodigoCUPS",
                    "Code": 0,
                    "CodeAlfa": "",
                    "Mensaje": "El cups ya esta asignado a contratos activos",
                    "IsAdvertencia": false,
                    "IsError": true,
                    "IsExcepcion": false,
                    "ExceptionTimestamp": "0001-01-01T00:00:00",
                    "ExceptionMessage": "",
                    "ExceptionStackTrace": "",
                    "InnerException": "",
                    "ImageSource": ""
                }
            ]
        }
    ]
}

How can I access to the 'Mensaje' option? 如何访问“ Mensaje”选项? I have this code where i make curl_init and its options: 我在执行curl_init及其选项的地方有以下代码:

$ch = curl_init($url.$contratoPotencial);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

I tried with this code: 我尝试使用以下代码:

echo "<br><br> Mensaje: ";
echo "<br>".$response->Contratos->Incidencias->Mensaje;

But it not works! 但这行不通!

You have mix of array and object in json: try below solution: 您在json中混合了数组和对象:请尝试以下解决方案:

$response = '{
    "Contratos": [
        {
            "IdCUPS": 0,
            "CodigoCUPS": "",
            "IdContrato": 0,
            "CodigoContrato": null,
            "IdCliente": 0,
            "IdDocumento": null,
            "IdEmpresa": null,
            "Incidencias": [
                {
                    "Propiedad": "CodigoCUPS",
                    "Code": 0,
                    "CodeAlfa": "",
                    "Mensaje": "El cups ya esta asignado a contratos activos",
                    "IsAdvertencia": false,
                    "IsError": true,
                    "IsExcepcion": false,
                    "ExceptionTimestamp": "0001-01-01T00:00:00",
                    "ExceptionMessage": "",
                    "ExceptionStackTrace": "",
                    "InnerException": "",
                    "ImageSource": ""
                }
            ]
        }
    ]
}';

$responseData = json_decode($response);
echo "<br><br> Mensaje: ";
echo "<br>".$responseData->Contratos[0]->Incidencias[0]->Mensaje;

Output 产量

Mensaje: 
El cups ya esta asignado a contratos activos

OR 要么

Encode complete json into array as below: 将完整的json编码为数组,如下所示:

$responseData = json_decode($response, true);
echo "<br><br> Mensaje: ";
echo "<br>".$responseData['Contratos'][0]['Incidencias'][0]['Mensaje'];

In both case you will get desired output 在两种情况下,您都将获得所需的输出

do a json_decode like below, 做如下的json_decode

$respData = json_decode($response, true);

and then print 然后打印

echo $respData['Contratos']['Incidencias']['Mensaje'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM