How to traverse a JSON array in PHP

1

I need to run a JSON array in PHP
Query URL: link

{
"coError": "0000",
"coRpta": null,
"feCaducidad": "05\/12\/2019",
"listaTramites": [
    {
        "nomTramite": "Rectificaci\u00f3n",
        "tiTramite": "R",
        "feTramite": "09\/11\/2011",
        "subTipoTramite": "B"
    },
    {
        "nomTramite": "Duplicado",
        "tiTramite": "D",
        "feTramite": "17\/11\/2010",
        "subTipoTramite": "B"
    }
]
}

The syntax has to be similar to this one in JAVA

if (resp.getStatusLine().getStatusCode() == 200) {
            this.v_jsnob_res = new JSONObject(EntityUtils.toString(resp.getEntity()));
            this.v_de_error = this.v_jsnob_res.getString("coError");
            System.out.print("co error " + this.v_de_error);
            if (this.v_de_error.equalsIgnoreCase("0000")) {
                this.v_fecha_caducidad = this.v_jsnob_res.getString("feCaducidad");
                System.out.print("fecha de caducidad  " + this.v_fecha_caducidad);
                this.v_lst_tramites = new ArrayList();
                JSONArray h2 = this.v_jsnob_res.getJSONArray("listaTramites");
                System.out.print("cantidad " + this.v_nu_cant);
                for (int i = 0; i < h2.length(); i++) {
                    JSONObject obj1 = h2.getJSONObject(i);
                    TramitesDni obj_tramDni = new TramitesDni();
                    obj_tramDni.setNomTramite(obj1.getString("nomTramite"));
                    obj_tramDni.setFecha(obj1.getString("feTramite"));
                    this.v_lst_tramites.add(obj_tramDni);
                }
                return Boolean.valueOf(true);
            }
    
asked by Eduardo Huaranga 09.12.2017 в 02:13
source

1 answer

3

The file_get_contents () function is an option, and then using the function json_decode () we decode it. Then it is easier to access the values oriented Objects.

$json = file_get_contents('http://servperu.ml/search/?dni=00000100');
$obj = json_decode($json);

$error = $obj->coError;
$fecha_caducidad = $obj->feCaducidad;
$tramites = $obj->listaTramites;
echo "Co Error : " . $error . "<br>";

if($error==="0000"){
  echo "Fecha Caducidad : " . $fecha_caducidad . "<br>";
  echo "Cantidad de Trámites : " . count($tramites) . "<br>";

  foreach ($tramites as$value) {
    echo "Número de Trámite  : " .$value->nomTramite . "<br>";
    echo "TI Trámite  : " .$value->tiTramite . "<br>";
    echo "Fecha de Trámite  : " .$value->feTramite . "<br>";
    echo "Sub Tipo de Trámite  : " .$value->subTipoTramite . "<br>";
  }
}
    
answered by 09.12.2017 / 02:47
source