JSON_Encode/JSON_Decode - Returns Stdclass Instead of Array in PHP

json_encode/json_decode - returns stdClass instead of Array in PHP

Take a closer look at the second parameter of json_decode($json, $assoc, $depth) at https://secure.php.net/json_decode

json_encode Change My Array Into stdClass Object, Is This Normal?

The "problem" has nothing in common with json_encode, but rather with json_decode.

This will return an array:

$array = json_decode($array, true);

use $result = json_decode(json_encode($result), true) to transfer array of std objects to array of arrays

Suggested by Scuzzy, I should use php.net/manual/en/function.json-last-error-msg.php to report the error.

Yes. there is an error when I do the json_encode.

Malformed UTF-8 characters, possibly incorrectly encoded

I used to run to this UTF-8 malformed issue frequently before, due to two reasons

  • Database tables were not utf8 encoded.
  • data transfer was in xml format instead of json format

But since the database' tables are mostly encoded as utf8 today, and more and more the data transfer in json format rather than xml, I had not run into utf8 characters issue for a long time.

Now go back to my issue, my tables are

ENGINE=InnoDB DEFAULT CHARSET=utf8 

I need to figure out why the query results from these tables still gave me utf8 Malformed problems.

Thanks!

PHP JSON decode - stdClass

You are accessing it with array-like syntax:

echo j_string_decoded['urls'][1];

Whereas object is returned.

Convert it to array by specifying second argument to true:

$j_string_decoded = json_decode($json_str, true);

Making it:

$json_str = '{"urls":["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}';

$j_string_decoded = json_decode($json_str, true);
echo j_string_decoded['urls'][1];

Or Try this:

$j_string_decoded->urls[1]

Notice the -> operator used for objects.

Quoting from Docs:

Returns the value encoded in json in
appropriate PHP type. Values true,
false and null (case-insensitive) are
returned as TRUE, FALSE and NULL
respectively. NULL is returned if the
json cannot be decoded or if the
encoded data is deeper than the
recursion limit.

http://php.net/manual/en/function.json-decode.php

PHP/JSON - stdClass Object

You can use get_object_vars() to get an array of the object's properties, or call json_decode() with json_decode($string,true); to get an associative array.


Example:

<?php
$foo = array('123456' =>
array('bar' =>
array('foo'=>1,'bar'=>2)));

//as object
var_dump($opt1 = json_decode(json_encode($foo)));

echo $opt1->{'123456'}->bar->foo;

foreach(get_object_vars($opt1->{'123456'}->bar) as $key => $value){
echo $key.':'.$value.PHP_EOL;
}

//as array
var_dump($opt2 = json_decode(json_encode($foo),true));

echo $opt2['123456']['bar']['foo'];

foreach($opt2['123456']['bar'] as $key => $value){
echo $key.':'.$value.PHP_EOL;
}
?>

Output:

object(stdClass)#1 (1) {
["123456"]=>
object(stdClass)#2 (1) {
["bar"]=>
object(stdClass)#3 (2) {
["foo"]=>
int(1)
["bar"]=>
int(2)
}
}
}
1
foo:1
bar:2

array(1) {
[123456]=>
array(1) {
["bar"]=>
array(2) {
["foo"]=>
int(1)
["bar"]=>
int(2)
}
}
}
1
foo:1
bar:2

json encode return as objects instead array

Even though that's not a valid JSON, you can replace the { with [

echo str_replace(array('{','}'),array('[',']'),json_encode($your_array)); 

Depending on the content you might need a more complex replacement with regular expressions.

json_decode to array

As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:

$result = json_decode($jsondata, true);

If you want integer keys instead of whatever the property names are:

$result = array_values(json_decode($jsondata, true));

However, with your current decode you just access it as an object:

print_r($obj->Result);


Related Topics



Leave a reply



Submit