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]);
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 have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.
I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....
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);