473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

JSON member access issue

I am using PHP with the JSON extension function json_decode.

I have a JSON with a member named "1" (ie) { "1":"somedata" }

Trying to access this via the -operator doesn't work, nor does
["1"].

Putting the JSON into a foreach loop DOES access the member:

foreach($json as $key=>$value) {
echo("$key<br />");
}
//outputs '1'

Is this an error on my part, an oversight in the PHP JSON
implementation, or something else? Why can foreach grab the members,
but I can't access them?

Thanks for your time!

Tyler
Jun 2 '08 #1
7 3018
On Tue, 22 Apr 2008 20:32:28 +0200, Logos <ty*********@gmail.comwrote:
I am using PHP with the JSON extension function json_decode.

I have a JSON with a member named "1" (ie) { "1":"somedata" }

Trying to access this via the -operator doesn't work, nor does
["1"].

Putting the JSON into a foreach loop DOES access the member:

foreach($json as $key=>$value) {
echo("$key<br />");
}
//outputs '1'

Is this an error on my part, an oversight in the PHP JSON
implementation, or something else? Why can foreach grab the members,
but I can't access them?

The problem is that while json_decode is able to extract it:
object(stdClass)#1 (1) {
["1"]=>
string(8) "somedata"
}

"1" is not a valid property name to use directly.

Workarounds:
Option 1, suitable for single known variable:
<?php
$var = json_decode('{ "1":"somedata" }');
$name = '1';
echo $var->$name;
?>

Option 2, suited for more generic processing:
<?php
function json_object_to_named_array($var){
if(!is_object($var)){
trigger_error('No object given');
return;
}
$return = get_object_vars($var);
foreach($return as &$value){
if(is_object($value)) $value = json_object_to_named_array($value);
}
return $return;
}
$test = array('foo' ='bar','foz' =array('fox' ='bax'));
$json = json_encode($test);
var_dump($json);
$var = json_decode($json);
var_dump($var);
$var = json_object_to_named_array($var);
var_dump($var);
?>
Output:
string(33) "{"foo":"bar","foz":{"fox":"bax"}}"
object(stdClass)#1 (2) {
["foo"]=>
string(3) "bar"
["foz"]=>
object(stdClass)#2 (1) {
["fox"]=>
string(3) "bax"
}
}
array(2) {
["foo"]=>
string(3) "bar"
["foz"]=>
array(1) {
["fox"]=>
string(3) "bax"
}
}
--
Rik Wasmus
Jun 2 '08 #2
On Apr 22, 3:14 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Tue, 22 Apr 2008 20:32:28 +0200, Logos <tyler.st...@gmail.comwrote:
I am using PHP with the JSON extension function json_decode.
I have a JSON with a member named "1" (ie) { "1":"somedata" }
Trying to access this via the -operator doesn't work, nor does
["1"].
Putting the JSON into a foreach loop DOES access the member:
foreach($json as $key=>$value) {
echo("$key<br />");
}
//outputs '1'
Is this an error on my part, an oversight in the PHP JSON
implementation, or something else? Why can foreach grab the members,
but I can't access them?

The problem is that while json_decode is able to extract it:
object(stdClass)#1 (1) {
["1"]=>
string(8) "somedata"

}

"1" is not a valid property name to use directly.

Workarounds:
Option 1, suitable for single known variable:
<?php
$var = json_decode('{ "1":"somedata" }');
$name = '1';
echo $var->$name;
?>

Option 2, suited for more generic processing:
<?php
function json_object_to_named_array($var){
if(!is_object($var)){
trigger_error('No object given');
return;
}
$return = get_object_vars($var);
foreach($return as &$value){
if(is_object($value)) $value = json_object_to_named_array($value);
}
return $return;}

$test = array('foo' ='bar','foz' =array('fox' ='bax'));
$json = json_encode($test);
var_dump($json);
$var = json_decode($json);
var_dump($var);
$var = json_object_to_named_array($var);
var_dump($var);
?>
Output:
string(33) "{"foo":"bar","foz":{"fox":"bax"}}"
object(stdClass)#1 (2) {
["foo"]=>
string(3) "bar"
["foz"]=>
object(stdClass)#2 (1) {
["fox"]=>
string(3) "bax"
}}

array(2) {
["foo"]=>
string(3) "bar"
["foz"]=>
array(1) {
["fox"]=>
string(3) "bax"
}}

--
Rik Wasmus
Ah, I thought it might be something like that then. The JSON notation
is perfectly fine, but PHP's grammar won't let me directly access the
incompatible JSON format. I shall just have to live with it then.

Thanks!

Tyler
Jun 2 '08 #3
Logos wrote:
Ah, I thought it might be something like that then. The JSON notation
is perfectly fine, but PHP's grammar won't let me directly access the
incompatible JSON format. I shall just have to live with it then.
:)

Try echo $var->{1};
Jun 2 '08 #4
On Apr 23, 6:18 pm, Alexey Kulentsov <a...@inbox.ruwrote:
Logos wrote:
Ah, I thought it might be something like that then. The JSON notation
is perfectly fine, but PHP's grammar won't let me directly access the
incompatible JSON format. I shall just have to live with it then.

:)

Try echo $var->{1};
YOU, sirrah, are my HERO! Thank you much much much!!!

:D

Tyler
Jun 2 '08 #5
Logos wrote:
I am using PHP with the JSON extension function json_decode.

I have a JSON with a member named "1" (ie) { "1":"somedata" }

Trying to access this via the -operator doesn't work, nor does
["1"].
Looks like you're already on your way, but just FYI, the json_decode
function takes an optional second argument that will, if true, cause
objects to be returned as associative arrays instead. Then, the usual
array notation (["1"]) should work.

Dave
Jun 2 '08 #6
On Wed, 30 Apr 2008 03:39:16 +0200, Dave Benjamin
<ra***@lackingtalent.comwrote:
Logos wrote:
>I am using PHP with the JSON extension function json_decode.
I have a JSON with a member named "1" (ie) { "1":"somedata" }
Trying to access this via the -operator doesn't work, nor does
["1"].

Looks like you're already on your way, but just FYI, the json_decode
function takes an optional second argument that will, if true, cause
objects to be returned as associative arrays instead. Then, the usual
array notation (["1"]) should work.
D'OH! Going through all that trouble writing a recursive function...
--
Rik Wasmus
Jun 2 '08 #7
On Apr 29, 6:39 pm, Dave Benjamin <ra...@lackingtalent.comwrote:
Logos wrote:
I am using PHP with the JSON extension function json_decode.
I have a JSON with a member named "1" (ie) { "1":"somedata" }
Trying to access this via the -operator doesn't work, nor does
["1"].

Looks like you're already on your way, but just FYI, the json_decode
function takes an optional second argument that will, if true, cause
objects to be returned as associative arrays instead. Then, the usual
array notation (["1"]) should work.

Dave
Keen - I may try that too. Good to know for future reference, in any
event!

Tyler
Jun 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
8
by: Douglas Crockford | last post by:
There is a new version of JSON.parse in JavaScript. It is vastly faster and smaller than the previous version. It uses a single call to eval to do the conversion, guarded by a single regexp test to...
3
by: asleepatdesk | last post by:
Hi, I need some help here. When I try to eval() my AJAX returned JSON string, I continually get a javascript error "Expected )". Here's my JSON string: {"recs": }; My js function simply...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
15
RMWChaos
by: RMWChaos | last post by:
As usual, an overly-long, overly-explanatory post. Better too much info than too little, right? A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my...
3
by: belred | last post by:
i tried a couple python json libraries. i used simplejson on the server and was using cjson on the client, but i ran into this issue. i'm now using simplejson on both sides, but i'm still...
15
RMWChaos
by: RMWChaos | last post by:
In my ongoing effort to produce shorter, more efficient code, I have created a "chicken and egg" / "catch-22" problem. I can think of several ways to fix this, none of them elegant. I want my code...
0
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.