[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

parsing from JSON format

Posted on 2011-04-20
2
Medium Priority
?
246 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:tomermes
2 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35440352
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

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 35440451
data.ResultSet.Results[0].uzip
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question