printar json api

Asked

Viewed 48 times

-1

I’m wanting to print a data q and json but when I open in the browser it opens different from what I see I’m using print_r wanted to know how to code

<?php
    $json='{"1":"a"}';

print_r(json_decode($json));

inserir a descrição da imagem aqui

how I wanted it to stay

inserir a descrição da imagem aqui

  • 1

    echo json_encode($json,JSON_PRETTY_PRINT); you are looking for?

  • echo json_encode($json) the JSON_PRETTY_PRINT is to make it more beautiful, only I don’t have time to explain in a reply, but there are some other possible parameters to make the formatting more beautiful

  • @Brunoh. I think you can’t understand if I give the command it returns me ""{"1":"to"}"" and I wanted it to return equal to the image below

1 answer

3


This view is the responsibility of the browser when it receives a JSON response from the server. Not all browsers display it like this.

For example, when accessing https://jsonplaceholder.typicode.com/users in Firefox is displayed:

inserir a descrição da imagem aqui

Already in Chrome is displayed:

inserir a descrição da imagem aqui

What makes this view enabled in Firefox is the header Content-Type of the server response, when it indicates that it is a JSON response.

inserir a descrição da imagem aqui

So to enable this view in Firefox just send the JSON reply:

<?php

header('Content-Type: application/json');
echo json_encode(...);

Browser other questions tagged

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