[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

PHP and Json

i am trying to format my json response so it is eayer to read.

$d = array('foo' => '0', 'baz' => 'long');

echo  json_encode($d), "\n";

works but returns all in a single row like

 {"foo":"500","baz":"long"}

but i would like it like this

 {
"foo":"500",
"baz":"long"
}
0
AlexPonnath
Asked:
AlexPonnath
  • 5
  • 3
1 Solution
 
Ray PaseurCommented:
<?php // RAY_temp_alexponnath.php
error_reporting(E_ALL);
echo '<pre>';

// A FUNCTION TO MAKE A FORMATTED DISPLAY
function json_display($j)
{
    $x = str_replace('","', '",' . PHP_EOL . '"', $j);
    $x = str_replace('{', '{' . PHP_EOL, $x);
    $x = str_replace('}', PHP_EOL . '}', $x);
    return $x;
}

// ORIGINAL DATA
$d = array('foo' => '500', 'baz' => 'long');

// JSON ENCODE
$j = json_encode($d);

// SHOW FORMATTED DISPLAY
echo json_display($j);

Open in new window

Please let me know if you have any questions, ~Ray
0
 
AlexPonnathAuthor Commented:
how about if I want to have a output like this

firstname
last name
     phonedata
         mobile
         home
         work
address
city
state
zip
country

I know I can do that easily in XML but not sure in Json
0
 
Ray PaseurCommented:
Where is the original data for that construct?  If you want to post the XML (or whatever) maybe we can show you how to format it.

But on a different note, why are you doing this?  JSON is a standard used for information transmission, not a display scheme.  Why not use print_r() or var_dump() to visualize the content of the pre-JSON variables?  That's what I would do.  Displaying JSON in a formatted string just seems like a lot of work without much added value in the results.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AlexPonnathAuthor Commented:
Ok, lets take the facebook example

{
   "name": "Facebook Developers",
   "is_published": true,
   "website": "http://developers.facebook.com",
   "username": "FacebookDevelopers",
   "company_overview": "Facebook Platform enables anyone to build social apps on Facebook, mobile, and the web.",
   "about": "Build and distribute amazing social apps on Facebook. https://developers.facebook.com/",
   "talking_about_count": 15965,
   "category": "Product/service",
   "id": "19292868552",
   "link": "http://www.facebook.com/FacebookDevelopers",
   "likes": 143883,
   "cover": {
      "cover_id": "10151008748223553",
      "source": "http://sphotos-a.xx.fbcdn.net/hphotos-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
      "offset_y": 0
   }
}

Open in new window

0
 
Ray PaseurCommented:
Thanks.  Where can I find the code to retrieve the exact same data as what you posted here?  I would like to start from whole cloth rather than from something that is already preformatted.
0
 
AlexPonnathAuthor Commented:
there is no unformatted code, this is the sample return from Facebook
0
 
Ray PaseurCommented:
OK, please post the URL for getting this from FB, thanks.
0
 
Ray PaseurCommented:
@AlexPonnath: Please see the grading guidelines here:
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

In order for us to help you we need to get answers to our questions.  It's been four days, and instead of posting the URL I asked for, you closed the question with a bad grade.  Going forward, please don't do that to the experts.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now