Link to home
Start Free TrialLog in
Avatar of AlexPonnath
AlexPonnathFlag for United States of America

asked on

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"
}
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexPonnath

ASKER

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
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.
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

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.
there is no unformatted code, this is the sample return from Facebook
OK, please post the URL for getting this from FB, thanks.
@AlexPonnath: Please see the grading guidelines here:
https://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.