Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 24th, 2007, 06:45 PM
Mike
Guest
 
Posts: n/a
Default json_decode() returns string type?

Hi,

I'm passing a JSON-encoded string to json_decode() and am expecting
its output to be an object type, but am getting a string type instead.
How can I return an object?

In the docs, the following returns an object:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));

However, if I json_encode() the string first and then call
json_decode(), the output is a string and not an object:

$json = json_encode('{"a":1,"b":2,"c":3,"d":4,"e":5}');
var_dump(json_decode($json));

This is just a simplified example--in practice what I'm doing is
pushing a JSON-encoded string to PHP via AJAX (or at least "AJ" ;).
However it does illustrate the problem of converting this encoded JSON
string to an object I can read in PHP (e.g., "$json->a").

How can I return an object type?

Thanks!

  #2  
Old August 24th, 2007, 07:15 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: json_decode() returns string type?

Mike wrote:
Quote:
Hi,
>
I'm passing a JSON-encoded string to json_decode() and am expecting
its output to be an object type, but am getting a string type instead.
How can I return an object?
>
In the docs, the following returns an object:
>
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
>
However, if I json_encode() the string first and then call
json_decode(), the output is a string and not an object:
>
$json = json_encode('{"a":1,"b":2,"c":3,"d":4,"e":5}');
var_dump(json_decode($json));
>
This is just a simplified example--in practice what I'm doing is
pushing a JSON-encoded string to PHP via AJAX (or at least "AJ" ;).
However it does illustrate the problem of converting this encoded JSON
string to an object I can read in PHP (e.g., "$json->a").
>
How can I return an object type?
>
Thanks!
>
It's already a json-encoded string. Why are you encoding it again?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles