How to output JSON data correctly using PHP

前端 未结 7 1512
时光说笑
时光说笑 2021-01-04 04:03

I\'m developing an Android app, and for the API I\'m sending my requests to a URL that should return JSON data.

This is what I\'m getting in my output:

7条回答
  •  [愿得一人]
    2021-01-04 04:26

    Your JSON should be like your first image; All in one line squished together. You can pass your output to jsonlint.com which can help you find your typo/bad data. ALL the current other answers could help you solve your issue as well. they all give you different options to format your output if you really want to do that.

    Here's another way for you to display your JSON in HTML.

    If you wrap your response in HTML < pre > tags you'll keep the white space format.

    For example, I use jQuery to get some json data and in jQuery take the json object and put it directly in a < pre > that's formatted

    [ results ]

    I use

    $('.output').html( JSON.stringify(data,null,4) );
    

    This will take your json return, format it with spaces with the HTML < pre > can use to format it like you want to see.

    If you want to have your json color coded... you can use http://prismjs.com/ or any other highlighting system...

提交回复
热议问题