I need to convert json file and pass it to php variables so that i can store them in the database.
<?php
//read the json file contents
$fo=fopen("dealdetails.json","r");
$fr=fread($fo,filesize("dealdetails.json"));
$array=json_decode($fr,true);
//$jsondata = file_get_contents('dealdetails.json');
//convert json object to php associative array
//$data = json_decode($jsondata, true);
echo $array;
echo $array['promo_id'];
echo "json not printed";
?>
I tried both file_get_contents and fopen, fread
Output :
Arrayjson not printed
I am not getting the value for promo_id instead when i printed the $array i am getting output as Array.
Please suggest. Thanks
Related
I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>
YTo2OntpOjA7czo0OiIyMDY3IjtpOjE7czo0OiIyMDY4IjtpOjI7czo0OiIyMDY5IjtpOjM7czo0OiIyMDcwIjtpOjQ7czo0OiIyMDcxIjtpOjU7czo0OiIyMDcyIjt9
The above is my encoded data.
When I try to decode it I am getting the output in this format:
a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}
How can I convert it into this
["2067","2068","2069","2070","2071","2072"]
or this
(2067,2068,2069,2070,2071,2072)
?
The data in your second block is serialised in PHP's object serialisation format. You can use the unserialize function to turn it back into a PHP variable:
$data = 'a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}';
$obj = unserialize($data);
echo json_encode($obj);
This outputs
["2067","2068","2069","2070","2071","2072"]
which is an array in the JSON format you requested.
Live demo: http://sandbox.onlinephpfunctions.com/code/6e9841c72950d44abd0ec6d45e2815cdcc89f42d
I using codeigniter. I want to retrive data from database and convert it into JSON object not JSON array.I'm using following code
public function json()
{
$content = $this->db->get('todolist'); //todolist table name
$data = $content->result_array();
echo json_encode($data);
}
Above code is converting database into JSON array.
Output
[{"todo_id":"1","todo_content":"Homework","date":"2016-05-05","iscomplete":null,"imagelink":"Lighthouse.jpg"},{"todo_id":"2","todo_content":"exam","date":"2015-04-21","iscomplete":null,"imagelink":"Desert.jpg"},{"todo_id":"3","todo_content":"Lab report","date":"2014-08-29","iscomplete":null,"imagelink":"FB_IMG_14700753538617403.jpg"}]
What will be best way to convert it into JSON object
Sangam try to understand the concept, check the following line:
$data = $content->result_array(); // $data is an array
echo json_encode($data); // here you are converting it into an json object
Your $data array contains more than one index in it that's why the json object is having multiple {} inside [];
You want to json_encode($data, JSON_FORCE_OBJECT).
The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.
Refer: PHP Array to Json Object
You can use JSON_FORCE_OBJECT see the example below.
echo json_encode($data, JSON_FORCE_OBJECT);
Assuming you're only getting one row back from your query.
change
echo json_encode($data);
to
echo json_encode($data[0]);
I am trying to output some json data from a json file via php but it does not seem to work. I tried this:
<?php
$jsonFile = file_get_contents('dataset/dataset.json');
$data = json_decode($jsonFile, true);
echo $data->{'data'}[0]->{'letter'}
?>
The json file is following:
{
"data":[
{
"letter":"A",
"blocks":{
"1":"0",
"2":"0",
"3":"0",
"4":"0",
"5":"0"
}
}
]}
Basically it should output the letter "A" but it outputs nothing. What did I do wrong?
Thanks
P.S. I tried to do it like here: How to process JSON in PHP? but it does not work.
After json_decode($jsonFile, true) your data is in array. So you should not access using object. Access data by array index. Try this..
echo $data['data'][0]['letter'];
More about json_decode()
This says, you get an array (the true parameter):
$data = json_decode($jsonFile, true);
You can see this if you do this:
print_r($data);
Try this:
echo $data['data'][0]['letter'];
I've this php code that passes results to a jquery getJSON function that doesn't work properly.
here is my code:
$data["total"]=mysql_num_rows($s);
while ($r = mysql_fetch_array($s))
{
$ris_sql["utente"]=tv($r["utente"]);
$ris_sql["contratto"]=tv($r["contratto"]);
$ris_sql["login"]=tv($r["login"]);
$ris_sql["piattaforma"]=tv($r["piattaforma"]);
$ris_sql["azione"]=tv(format_valid($r["azione"]));
$ris_sql["data"]=tv($r["data"]);
$ris_sql["note"]=tv(str_replace("<br>","",$r["note"]));
$riga[]=$ris_sql;
}
$data["rows"]=json_encode($riga, JSON_FORCE_OBJECT);
echo json_encode($data);
If I try to use firebug I see that my rows elements in JSON result like a string instead a series of objects, what is wrong in my code??
You are double-encoding the $riga JSON. Don't separately encode the $riga part prior to encoding the rest of the $data array you're sending back to the browser.
// Instead of
$data["rows"] = json_encode($riga, JSON_FORCE_OBJECT);
// This double-encodes the contents of $data before outputting back to the browser or ajax call
echo json_encode($data);
// Just do:
echo json_encode($data, JSON_FORCE_OBJECT);
If the format isn't what you expect, instead you can cast $riga from an array to an object and then encode the whole thing without JSON_FORCE_OBJECT.
// Cast $riga from an assoc array to an instance of stdClass
$data['rows'] = (object)$riga;
// And then JSON encode the whole $data
echo json_encode($data);