Link to home
Start Free TrialLog in
Avatar of tomermes
tomermes

asked on

parsing from JSON format

given the following query:
{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":40,"Found":1,"Results":[{"quality":40,"latitude":"37.777125","longitude":"-122.419644","offsetlat":"37.777125","offsetlon":"-122.419644","radius":10700,"name":"","line1":"","line2":"San Francisco, CA","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"","city":"San Francisco","county":"San Francisco County","state":"California","country":"United States","countrycode":"US","statecode":"CA","countycode":"","uzip":"94102","hash":"","woeid":2487956,"woetype":7}]}}
how do I get the "uzip"?
alert(data.ResultSet.Results) works
but alert(data.ResultSet.Results.uzip)  says 'undefined'
and so does alert(data.ResultSet.Results[uzip]).
thanks.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can see this in action at http://www.laprbass.com/RAY_temp_tomermes.php

If you want an alert, you might want to echo the JavaScript into the browser output stream.  This script just echoes the data, but it would not be too hard to add the JS.

HTH, ~Ray
<?php // RAY_temp_tomermes.php
error_reporting(E_ALL);

// JSON-ENCODED STRING FROM THE POST AT EE
$json = <<<ENDJSON
{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":40,"Found":1,"Results":[{"quality":40,"latitude":"37.777125","longitude":"-122.419644","offsetlat":"37.777125","offsetlon":"-122.419644","radius":10700,"name":"","line1":"","line2":"San Francisco, CA","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"","city":"San Francisco","county":"San Francisco County","state":"California","country":"United States","countrycode":"US","statecode":"CA","countycode":"","uzip":"94102","hash":"","woeid":2487956,"woetype":7}]}}
ENDJSON;

// RECOVER THE OBJECT
$thing = json_decode($json);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// echo "<pre>";
// var_dump($thing);

// FIND THE UZIP
foreach ($thing->ResultSet->Results as $a)
{
    $z = $a->uzip;
    echo "UZIP IS $z";
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark 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