json_decode not working on string variable - php

I'm trying to decode a json string into a php array and I'm getting a syntax error (4) when I use json_last_error().
The string comes from an API call to an Infusionsoft database. The database returns an array with one key/value pair. The value contains the json string.
Array ( [_PMSChargeItems] => [{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}] )
When I save the value to a string variable and pass it to json_decode() I get the syntax error (it doesn't work). If I pass the entire json string itself (copy and paste from echoing the string var value) to json_decode() the array is created (it works).
I have validated the json string at both http://jsonlint.com/ and http://www.functions-online.com/json_decode.html.
How can I get the string to be correctly decoded when it is passed to the json_decode function using a variable?
Here's the code I am using to decode:
$cid = $_GET['Id'];
$returnFields = array('_PMSChargeItems');
$conDat = $appConnect->dsLoad("Contact", $cid, $returnFields);
$a = $conDat['_PMSChargeItems'];
var_dump(json_decode($a, true));
echo json_last_error();
Here's the result of var_dump($a) before the decode:
string(4649) "[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]"
I've tried to find hidden characters using html_entities and html_entity_decode and didn't find any. I also confirmed using mb_detect_encoding that the string has all ASCII characters.
I just tried the following:
var_dump(json_decode(strval($a), true));
The result was NULL and the decode error was the syntax error.
What's curious is that var_dump on ($a) gives me a character count of 4649 whereas strlen('[("Date"....) of the string itself gives a character count of 2789...

Thanks to all of your help I solved the puzzle!
When the JSON string was returned from the Infusionsoft database all of the quotes were being stored as $quot; instead of as double quotation marks.
I found this out by saving the string to a .txt file (using fopen, fwrite, and fclose) and opening the file with a text editor. I had been figuring that I would need to look at the file with a hex editor (as was recommended by Ryan - I think that's his name anyway, he deleted his comment that contained the suggestion so I'm not sure) but it turns out that wasn't necessary. Here's what the data looked like in the file:
[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},....
To remove the quotes I used htmlspecialchars_decode() on the string and then was able to successfully pass the string into json_decode. Here's the code:
$a = $conDat['_PMSChargeItems'];
$b = htmlspecialchars_decode($a);
$c = json_decode($b);
Thanks again for your helpful comments and answers!

EDIT
You are calling json_decode on an array that isn't json encoded, you need to call json_decode on the string that is json_encoded, i.e. the element "_PMSChargeItems" of $a
This seems to work:
$a = '[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]';
print_r( json_decode( $a ) );
or the array way:
$a = array( '_PMSChargeItems' => '[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]' );
print_r( json_decode( $a['_PMSChargeItems'] ) );
You seem to mix up JavaScript and PHP

Related

Serilaizing in PHP returns wrong value

I try to serialize my data with PHP. Unfortunaly, the serialize() function returns a wrong value.
String to be serialized:
{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}
What serialize() returns on my server:
serialize($code);
s:204:"{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}";
What should be returned (https://duzun.me/playground/serialize):
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
You need to json_decode it first to get the wanted result:
When you use the boolean switch as second parameter in json_decode it will be an array instead of an object.
$serialized = serialize(json_decode($inputString, true));
echo $serialized;
// output:
// a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
The site you're using isn't clear about what it's doing, but it appears to be treating the string as JSON and decoding to an array before serializing it as PHP. If you want to replicate this, you can use:
$str = '{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}';
echo serialize(json_decode($str, true));
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
As pointed out in the comments, unless there's a specific reason you need serialized PHP, then just stick with the serialized JSON string you already have - it'll be both more readable and portable.

PHP: Getting associative array returns null, despite existing in var_dump

I have an array of 'servers' that I'm storing in a JSON file.
The JSON file looks like this:
{"1":{"available":1,"players":0,"maxplayers":4}}
I retrieve this value with this:
$servers = (array) json_decode(file_get_contents("activeservers.json"));
However, when I try to access the array with $server = $servers[$id], $server is null. I noticed that the key is in quotes, so I also tried casting $id to a string, and surrounding it with quotes (") which didn't work.
Something to note, is that this code is returning "NULL":
foreach(array_keys($servers) as $key){
var_dump($servers[$key]);
}
Your code is wrong. Also you don't need to type cast when doing a json_decode, you can instead set the second parameter to true more info here.
Also you don't need to use the array_keys function in your foreach loop,
try this.
$json = '{"1":{"available":1,"players":0,"maxplayers":4}}';
$servers = json_decode($json, true);
foreach($servers as $key => $value) {
print $value["available"];
}
Do a print_r($value) to get all the array keys available to use. Also you could take advantage of the $key variable to print out the array key of the parent array.
Thanks, #Rizier123 (who solved the question).
Apparently passing TRUE as the second parameter to my json_decode function fixes the issue.
After checking the PHP documentation for json_decode() (PHP: json_decode), it seems that passing this parameter means that the resulting decoded array is automatically converted into an associative array (and this is recurring, meaning that this automatically happens for sub-arrays).
Edit: #Rizier123 also says that "you might want to read: stackoverflow.com/a/10333200 to understand a bit better why it is so "weird" and your method didn't work properly."

Parsing twitter stdClass string in PHP

I have this string stored in a database column:
O:8:"stdClass":2:{s:4:"type";s:5:"Point";s:11:"coordinates";a:2:{i:0;d:-23.5663719;i:1;d:-46.65284157;}}
And I just want the "coordinates" in the middle:
-23.5663719,-46.65284157
There is a easy way to parse it in PHP to get this value?
I tried json_decode but it didn't work.
Thanks.
It's not json encoded, but serialized using serialize().
So what you have to do is:
$o = unserialize($str); // where $str is a string fetched from DB
var_dump($o->coordinates);
References:
http://www.php.net/manual/en/function.serialize.php
http://www.php.net/manual/en/function.unserialize.php

Get string value of array on remote server

I'm trying to find the content of an array on a remote server. All I can send back currently is strings (due to limitations in the PHP implementation of xmlrpc). Normally, I'd just use var_dump(), but that returns void. I've tried using var_export, but I get XML errors, even when I cast the result to a string.
How can I get the string representation of an array?
Use serialize():
$string = serialize( $array);
Then use unserialize() to get it back into an array:
$array = unserialize( $string);
You can also use json_encode() / json_decode() if you're interested in a JSON formatted string.
$string = json_encode( $array);
$array = json_decode( $string);
You can use PHP serialize, json_encode .... These are alternate methods to encode an object.

Looping through a JSON string

I am trying to loop through a JSON string using a foreach(). However I keep getting the following error:
"Notice: Trying to get property of non-object".
The strange part is that when I copy and paste the JSON string and then run the foreach(), it work fine. Just to provide some detail, I am using the Best-Buy API.
Since this seemed to work fine for everyone here is it possible there is something wrong with the data that best buy is feeding me?
Please help, I have tried everything!
UPDATE sorry for not posting code. here it is:
$info = json_decode($test, true);
function tagGen($info){
foreach($info as $key => $value){
}
As you have not posted the code so we can just think that you have a string jason encoded. My dear json encoded string is some thing which is some sort of javascript form. And foreach is a php loop. So if you have a json encoded string and you want to use it in foreach loop you have to use json_decode function for that.
When you will apply json_decode you will be having a string From the documentation
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.
For some example code.
$str=json_decode($yourjson);
foreach($str as $key=>$value)
{}
You are probably not using json_decode on the JSON string. Take a look at how I would do this:
$json = "somejsonstring";
$json_array = json_decode($json, true);
foreach($json_array as $element) {
echo $element['some key'];
}
Note the "true" second parameter given to the json_decode method. That returns an associative array rather than a standard PHP object. Makes it a lot easier to work with in foreach loops.
Hope that helps, although your question should really include a code sample.
Print your json string somewhere you can copy it and then check if it is valid using this tool: http://jsonformatter.curiousconcept.com/

Categories