0

Tengo una tabla en una base de datos que tiene como contenido archivos json. Con php hago consulta para obtener el json que se ve así (obtenido con print_r()):

{"platform":"Moodle","contextActivities":{"category":[{"id":"http://vocab.xapi.fr/categories/learning-unit","objectType":"Activity","definition":{"type":"http://vocab.xapi.fr/activities/granularity-level"}},{"objectType":"Activity","id":"http://vocab.xapi.fr/categories/vle-profile","definition":{"type":"http://adlnet.gov/expapi/activities/profile"}}],"grouping":[{"objectType":"Activity","id":"http://www.di.panconpalta.com.mx","definition":{"type":"http://vocab.xapi.fr/activities/system"}}],"parent":[{"objectType":"Activity","id":"http://www.di.panconpalta.com.mx/xapi/activities/course/5b24f650-3234-47d1-9c2d-b09d5d08557c","definition":{"type":"http://vocab.xapi.fr/activities/course"}}]},"extensions":{"http://vocab.xapi.fr/extensions/platform-event":"\\mod_hvp\\event\\course_module_viewed"}},"timestamp":"2019-07-16T17:13:22-04:00","actor":{"objectType":"Agent","account":{"homePage":"http://www.di.panconpalta.com.mx","name":"77e56417-3e9a-4d1a-840b-b89cce9710c8"}},"verb":{"id":"http://vocab.xapi.fr/verbs/navigated-in"},"object":{"objectType":"Activity","id":"http://www.di.panconpalta.com.mx/xapi/activities/hvp/c3c2ffec-74cf-4e51-ba77-2cfacd276b38","definition":{"type":"http://vocab.xapi.fr/activities/interactive-video","name":{"es":"Estrategias de apoyo"},"extensions":{"http://vocab.xapi.fr/extensions/platform-concept":"hvp-interactive-video","http://vocab.xapi.fr/extensions/concept-family":"resource"}}},"stored":"2019-07-16T21:13:22.4585Z","authority":{"objectType":"Agent","account":{"homePage":"http://trax.test","name":"traxlrs"}},"id":"7b33d7e2-46e4-39dc-b4c6-e1d7473c64a2","version":"1.0.0"}

Luego, decodifico el contenido como un archivo json con la función json_decode(). Entonces el json queda así:

stdClass Object ( [context] => stdClass Object ( [platform] => Moodle [contextActivities] => stdClass Object ( [category] => Array ( [0] => stdClass Object ( [id] => http://vocab.xapi.fr/categories/learning-unit [objectType] => Activity [definition] => stdClass Object ( [type] => http://vocab.xapi.fr/activities/granularity-level ) ) [1] => stdClass Object ( [objectType] => Activity [id] => http://vocab.xapi.fr/categories/vle-profile [definition] => stdClass Object ( [type] => http://adlnet.gov/expapi/activities/profile ) ) ) [grouping] => Array ( [0] => stdClass Object ( [objectType] => Activity [id] => http://www.di.panconpalta.com.mx [definition] => stdClass Object ( [type] => http://vocab.xapi.fr/activities/system ) ) ) [parent] => Array ( [0] => stdClass Object ( [objectType] => Activity [id] => http://www.di.panconpalta.com.mx/xapi/activities/course/5b24f650-3234-47d1-9c2d-b09d5d08557c [definition] => stdClass Object ( [type] => http://vocab.xapi.fr/activities/course ) ) ) ) [extensions] => stdClass Object ( [http://vocab.xapi.fr/extensions/platform-event] => \mod_hvp\event\course_module_viewed ) ) [timestamp] => 2019-07-16T17:13:22-04:00 [actor] => stdClass Object ( [objectType] => Agent [account] => stdClass Object ( [homePage] => http://www.di.panconpalta.com.mx [name] => 77e56417-3e9a-4d1a-840b-b89cce9710c8 ) ) [verb] => stdClass Object ( [id] => http://vocab.xapi.fr/verbs/navigated-in ) [object] => stdClass Object ( [objectType] => Activity [id] => http://www.di.panconpalta.com.mx/xapi/activities/hvp/c3c2ffec-74cf-4e51-ba77-2cfacd276b38 [definition] => stdClass Object ( [type] => http://vocab.xapi.fr/activities/interactive-video [name] => stdClass Object ( [es] => Estrategias de apoyo ) [extensions] => stdClass Object ( [http://vocab.xapi.fr/extensions/platform-concept] => hvp-interactive-video [http://vocab.xapi.fr/extensions/concept-family] => resource ) ) ) [stored] => 2019-07-16T21:13:22.4585Z [authority] => stdClass Object ( [objectType] => Agent [account] => stdClass Object ( [homePage] => http://trax.test [name] => traxlrs ) ) [id] => 7b33d7e2-46e4-39dc-b4c6-e1d7473c64a2 [version] => 1.0.0 ) 

Y cuando intento obtener un valor para insertarlo en una tabla, no aparece nada. A continuación el extracto de código:

                            <tbody>
                        <?php
                        $sql = "select * from trax_xapiserver_statements order by id DESC";

                        $estado = ejecutarComando($sql);
                        $num=0;
                        while($row = $estado->fetch_assoc()) { 
                            $array = json_decode($row["data"]);
                        ?>

                        <tr class="odd gradeX">
                            <td><?php echo $num+=1;?></td>
                            <td><?php echo substr($row["data"], 0, 100)."...";?></td>
                            <td><?php echo $array->objectType;?></td> //Aquí es donde debería mostrarse el resultado ObjectType
                            <td><?php echo $row["voided"];?></td>
                            <td><?php echo $row["created_at"];?></td>
                            <td><?php echo $row["updated_at"];?></td>
                            <td style="text-align:center">
                                <div class="btn-group">
                                    <a href="view.php?id=<?php echo $row['id'];?>" class="btn btn-default"><i class="glyphicon glyphicon-eye-open"></i></a>
                                    <a href="download.php?id=<?php echo $row['id'];?>" class="btn btn-default"><i class="glyphicon glyphicon-cloud-download"></i></a>
                                </div>
                            </td>
                        </tr>

                        <?php
                        }
                        ?>
                        </tbody>

En resumen, necesito obtener cada uno de los valores del array generado del json y no puedo, además que no sé exactamente lo que es stdClass.

1 respuesta 1

1

Las stdClass son un tipo de variable que deben ser manipuladas como objetos. Es por esto que muchas veces son dificiles de manipular.

EJ:

//Si queremos obtener el parametro ["voided"] **NO** debemos hacer $var["voided"]
//Debemos hacer $var->voided

Mi recomendacion personal es que hagas

json_decode($tuJson, true);

Esto convertira tu json en un array associativo que es mucho mas facil de manipular, ya que es un array de toda la vida, pero con keys alfanumericas.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.