How to display in the browser and in a friendly way information from a JSON file?

Asked

Viewed 895 times

5

The application receives a JSON file, already validated, and must display it to the user who is a programmer.

I would like to display JSON as on this validation and beautify site JSON.

So I wanted this JSON:

{"glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"}}}}
}

It was displayed the way below to make it more readable to the programmer, like this: inserir a descrição da imagem aqui

Note that there is the possibility to expand or not the node.

Does anyone know any application that does this?

Note: I’m using php.

  • In Laravel has an auxiliary method dd() that displays JSON on the indented screen, tries to take a look at the code, think it can help.

  • @zwitterion has to be in json? If it is array I have an alternative. If you want I can post a reply

  • Hi @Djalmamanfrin yes can only be a JSON. But if you want to put there the code for the array. Maybe someone needs or even me in another scenario.

2 answers

5

If you are using a 5.4+ version of PHP, you can use the constant JSON_PRETTY_PRINT of the method json_econde(). Following example:

$a = ['cor'=> 'azul', 'largura' => '120', 'modelo' => 'A'];
echo json_encode($a, JSON_PRETTY_PRINT);

More information on: http://php.net/manual/en/function.json-encode.php

1


install via commiserate the package var-Dumper.

add the line:

dd(json_decode($seu_json));

If the data comes in array just pass the parameter inside the dd():

dd($seu_array);

outworking:

inserir a descrição da imagem aqui

This way you can minimize and maximize on-screen information

  • 1

    Good! My option doesn’t do this, just "printa" in an organized way.

  • Help the hell out in the threshing room

  • 1

    Very good this option and it was what I was looking for. In the project I am working now is not Laravel. How will it work this?

  • This package does not depend on the Larable. It has been extracted. You can do a test, create a project, install the package via commiserate, assign the autoload.php on the page, create an array and apply a dd($array) as a test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.