Reputation: 21
I have been at this for hours and cant for the life of me figure out how to decode this json file. I am using gnip to capture twitter data. Gnip is returning the data in JSON format. I need a way to decode this in php and be able to access each of the json objects. I have worked with json before but I am not able to wrap my mind around this one. One funny thing i came across during simple testing was if i have a json code like this
{"foo":123}
I can decode this using the json_decode() function. But if the json is formatted like this
{
"foo":123
}
it doesnt decode :/
Here is a link to the json code:
https://dl-web.dropbox.com/get/Public/myjson.json?w=b7ad8e2c
if this link doesnt work this should
https://dl.dropbox.com/u/57604377/myjson.json
I tried to access it like this.
i put all of this json code inside a file and read the file and put the contents into a variable called $json then,
$obj = json_decode($json)
i tried both these methods to access the value of "id" for starters
echo $obj->id
and
echo $obj->{'id'}
it doesnt return anything
I hope some of you brilliant people can give me some insights into this.
Upvotes: 2
Views: 278
Reputation: 3663
Given this code in a file called json.php:
<?
$json = file_get_contents("php://stdin");
$obj = json_decode($json);
var_dump($obj);
// Different echos go here
?>
And cat'ing in your test file
cat multiline_gnip.json | php json.php
Then the following work for me:
echo $obj->id
echo $obj->{id}
echo $obj->{'id'}
Changing the json_decode line to:
$obj = json_decode($json, true);
Gives me back an associative array and then this works:
echo $obj["id"]
Mind sharing some of the code you're using for parsing this?
And for completeness here is my php --version
PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
(Disclaimer: I work for Gnip)
Upvotes: 1
Reputation: 3871
You should use php cli to test your functions & data. It should be fairly straightforward to spot your bug from there.
Upvotes: 0