Convert string to object or array in PHP

2

This string returns it when you send the data to a provider:

string(474) "
{
     "status":"success",
     "Data":
        {
        "RazonSocial":"Ferreteria Perez",
        "RFC":"XAXX010101006",
        "Calle":"Av.Juarez",
        "Numero":"1234",
        "Interior":"",
        "Colonia":"Centro",
        "CodigoPostal":"44640",
        "Ciudad":"Guadalajara",
        "Delegacion":"Guadalajara",
        "Estado":"Jalisco",
        Pais":"M\u00e9xico",
        "Contacto":{
              "Nombre":"Jos\u00e9 Ram\u00f3n",
              "Apellidos":"P\u00e9rezL\u00f3pez",
              "Email":"[email protected]",
              "Email2":"",
              "Email3":"",
              "Telefono":"33 3877 0000"},
              "UID":"5bbecdf98df8c",
              "cfdis":0,
              "cuentas_banco":[]
        }
    }"

I would like to convert it to an array or an object. I'm only interested in the answer:

"UID":"5bbecdf98df8c"

Y:

"status":"success",

I've already tried it and I can not convert it with PHP.

    
asked by daniel 11.10.2018 в 07:11
source

2 answers

3

I think that for your case, we can keep the JSON directly, since when passing TRUE the original object is reconverted to array. We would then be talking about a second unnecessary conversion .

Also, the code is written in a more elegant way (in my opinion):

$string ='{
 "status":"success",
 "Data":
    {
    "RazonSocial":"Ferreteria Perez",
    "RFC":"XAXX010101006",
    "Calle":"Av.Juarez",
    "Numero":"1234",
    "Interior":"",
    "Colonia":"Centro",
    "CodigoPostal":"44640",
    "Ciudad":"Guadalajara",
    "Delegacion":"Guadalajara",
    "Estado":"Jalisco",
    "Pais":"M\u00e9xico",
    "Contacto":{
          "Nombre":"Jos\u00e9 Ram\u00f3n",
          "Apellidos":"P\u00e9rezL\u00f3pez",
          "Email":"[email protected]",
          "Email2":"",
          "Email3":"",
          "Telefono":"33 3877 0000"
    },
    "UID":"5bbecdf98df8c",
    "cfdis":0,
    "cuentas_banco":[]
  }
}';


$json = json_decode($string);
echo $json->status;
echo $json->Data->UID;

Exit:

success
5bbecdf98df8c
    
answered by 11.10.2018 / 11:25
source
2

A Pais ( Pais":"M\u00e9xico", ) is missing a " at the beginning to be a correct json.

Use json_decode :

<?php
 $string ='{
 "status":"success",
 "Data":
    {
    "RazonSocial":"Ferreteria Perez",
    "RFC":"XAXX010101006",
    "Calle":"Av.Juarez",
    "Numero":"1234",
    "Interior":"",
    "Colonia":"Centro",
    "CodigoPostal":"44640",
    "Ciudad":"Guadalajara",
    "Delegacion":"Guadalajara",
    "Estado":"Jalisco",
    "Pais":"M\u00e9xico",
    "Contacto":{
          "Nombre":"Jos\u00e9 Ram\u00f3n",
          "Apellidos":"P\u00e9rezL\u00f3pez",
          "Email":"[email protected]",
          "Email2":"",
          "Email3":"",
          "Telefono":"33 3877 0000"
    },
    "UID":"5bbecdf98df8c",
    "cfdis":0,
    "cuentas_banco":[]
  }
}';


$array = json_decode($string, true);
var_dump($array['status'],$array['Data']['UID']);

Exit:

string(7) "success"
string(13) "5bbecdf98df8c"

The true as the second parameter decodes it as an array, otherwise it gives you an object.

    
answered by 11.10.2018 в 07:29