I'm trying to parse a json string extracted from a MySQL database. The json string contains a two-dimensional array, but json_decode returns null and json_last_error returns 0 which I assume means no error occurred.
$result is the json string
json_decode($result, true);
The string:
[
["17544500374","17544500489","17544500571","17544500587","17544500528"],
["17544500651","17544500432","17544500673","17544500452","17544500362"],
["17544500454","17544500457","17544500523","17544500441"],
["17544500547","17544500463","17544500535","17544500676"],
["17544500548","17544500581","17544500584","17544500382"],
["17544500593","17544500364","17544500660","17544500595"],
["17544500635","17544500647","17544500529","17544500670"]
]
You do not have a key for your values. That means, your second parameter is not correct, because the function is unable to create an associative array.
This works fine on my mashine:
json_decode($result);
The $result must be a string:
$result = '[["17544500374","17544500489","17544500571","17544500587","17544500528"],["17544500651","17544500432","17544500673","17544500452","17544500362"],["17544500454","17544500457","17544500523","17544500441"],["17544500547","17544500463","17544500535","17544500676"],["17544500548","17544500581","17544500584","17544500382"],["17544500593","17544500364","17544500660","17544500595"],["17544500635","17544500647","17544500529","17544500670"]]'
Related
I know there already are questions like this, but It didn't help me.
I get the follow error on my site:
Warning: Illegal string offset 'networkConnections' in
/var/www/bitmsg/templates/header.php on line 25 {
The line is
<?= $bmstatus["networkConnections"] ?> p2p nodes
if I print_r $bmstatus, then I get:
{
"numberOfBroadcastsProcessed": 2308,
"networkStatus": "connectedAndReceivingIncomingConnections",
"softwareName": "PyBitmessage",
"softwareVersion": "0.4.1",
"networkConnections": 52,
"numberOfMessagesProcessed": 22888,
"numberOfPubkeysProcessed": 8115
}
How to I fetch the information from this array?
I've tried both $bmstatus['networkConnections'] and $bmstatus->networkConnections
but both is returning that error?
$bmstatus contains a JSON string. You have to decode it first to be able to extract the required information out of it. For this purpose, you can use the built-in function json_decode() (with the second parameter set as TRUE to get an associative array, instead of an object):
$json = json_decode($bmstatus, true);
echo $json['networkConnections'];
It's a json string. You need to decode your json response using json_decode with second parameter true to get as an associative array.
$bmstatusArray = json_decode($bmstatus,true);
echo $bmstatusArray["networkConnections"];
I would like to have the data returned from mtgapi to be placed into an array of associative keys and values.
Sample output can be found at http://mtgapi.com/docs.
As the output is returned as a string, I pull it into PHP as a string using file_get_contents($url);
Edit:
I did not know the return is a JSON object! Simple!
It's returning a JSON object. You should use json_decode.
Try:
$json = file_get_contents($url);
var_dump(json_decode($json, true));
source
This question already has answers here:
json_encode/json_decode - returns stdClass instead of Array in PHP
(7 answers)
Closed 9 years ago.
I am using json_encode() to encode array into json format. but it returning object instead of array. I want to return an array not an object.
any body have any idea?
Basically json_decode() will return two types of data.
1) Object
2) Associative array
By default, json_decode() returns object type value.
But, if you want value as an array format you must use TRUE as a second argument in json_decode().
e.g,
$decoded_value = json_decode($json_encoded_value, TRUE);
use this code for decoding your encode json data
$encode = $your_json_encoded_data
json_decode($encode, TRUE);
actually json_encode function in php will return a json formatted string.
and if you want to parse json formatted string back in php
then you should use json_decode.
json_decode function will return data two types.
object & associtavie array.
json_decode(); return type object
json_decode(, TRUE); return type associtative array
You should use json_decode with TRUE param like following example:
$array = array(1,2,3);
$encode = json_encode($array);
$decode = json_decode($encode, TRUE);
Now $decode is array, not object.
I am new to PHP. I am using json_encode to convert an array into json data, and decode it using json_decode in another file. However, I am getting json error as syntax error.
My code is as follows:
File 1:
$result = get_data_array();
exit(json_encode($result));
File 2:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
$data->name // name is the array key
However, I am getting an error as:
Trying to get property of non-object.
You passed true to the second parameter of json_decode so it will return an array.
Use this:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
echo $data['name'];
I was having a question about making a 2D JSON string
Now I would like to know why I can't access the following:
$json_str = '{"urls":["http://example.com/001.jpg","http://example.com/003.jpg","http://example.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}';
$j_string_decoded = json_decode($json_str);
// echo print_r($j_string_decoded); // OK
// test get url from second item
echo j_string_decoded['urls'][1];
// Fatal error: Cannot use object of type stdClass as array
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
json_decode by default turns JSON dictionaries into PHP objects, so you would access your value as $j_string_decoded->urls[1]
Or you could pass an additional argument as json_decode($json_str,true) to have it return associative arrays, which would then be compatible with $j_string_decoded['urls'][1]
Use:
json_decode($jsonstring, true);
to return an array.