I want to use json_decode() to decode a string stored in a database like this:
{"results":[{"r":"1","c":"0"},{"r":"2","c":"0"},{"r":"3","c":"0"}]}
The problem is: the function returns NULL.
But when I try the following code, everything works:
$data ='{"results":[{"r":"1","c":"0"},{"r":"2","c":"0"},{"r":"3","c":"0"}]}';
$JO=json_decode($data);
var_dump($JO);
The value returned from the database is exactly the same as I described above.
For all the people who might get the same problem, my problem was that when I was getting data from database to display it, I was adding html style and table parameters to the data, after removing them everything went back to normal.
Related
i have a problem with the new implementet JSON storage in MySQL.
I am using mysql-5.7.12, jquery-2.1.1 and bootstrap-editable-1.5.1
The code looks like this:
<dt>Memo</dt>
foreach(json_decode($row['memo'], true) as $user => $memo) {
// $row['memo'] is something like: {"user":"very-long-text-with-some\\n escaped lines..."}
echo "
<dd><a class='memo' id='telephone-memo' data-pid='".$row['id']."' data-text='".$memo."' employee='".$user."'>new</a></dd>";
}
The a class='memo' ... is the x-editable.
A look inside the database shows no error. I can store more than enough inside the JSON string.
The problem is if i store more than 194 chars in the x-editable textarea, the data-text attribute shows empty! If this accures the user will overwrite his data with the next edit, because he/she did'nt see the actual stored data. Is there a limitation i did'nt know from?
I am exceedingly grateful for your help.
The Problem is solved!
I'm using since many years a UCFIRST() function in MySQL.
This is a 8-bit function!!
Normally used for names ect.
Function for the JSON-column removed and it works.
I am creating an array of pages using the following query:
$pages = Page::orderBy('sorting')->get()->toArray();
When I then json_encode the output, the output is corrupted when one of the page titles has a quote in it. How can I prevent this?
If you want to return json, you should use:
$pages = Page::orderBy('sorting')->get()->toArray();
return response()->json(['pages' => $pages]);
it should not make any problems. In case you have any, please show what they are.
I've experienced problems with json only when I have DB connection non-UTF and have their some characters. As json_encode needs data be in UTF-8 it might cause problems
Laravel is indeed returning a correct JSON. It seemed the javascript needed to call the object in a weird style, where the object needed to be placed within single quotes: UINestable.init('{ $json) !!}'). So when a single quote was within the JSON it would fault. Thanx though
I'm using PHP5 and OCI 8 with Oracle 11g.
When I fetch a row using oci_fetch_all, the whole result is converted as STRING even for the NUMBER columns and even if I use Oracle's TO_NUMBER in the query.
What I'm trying to do is simple: the javascript calls the PHP script through an Ajax request. The script just fetch some NUMBER data and encode them into JSON. I want the data to be encoded as integer, so the javascript can do math stuff on it (add, divide,..etc) without any conversion.
I am pretty sure that the problem comes from OCI and not JSON encoding because when I VAR_DUMP the result of oci_fetch_all, I can clearly see double quotes on every result:
{
"COLUMN1":"12",
"COLUMN2":"52"
}
I want the result to look like this:
{
"COLUMN1":12,
"COLUMN2":52
}
I tried to:
Change the flag of oci_fetch_all (OCI_FETCHSTATEMENT_BY_ROW, OCI_FETCHSTATEMENT_BY_COLUMN...)
Use oci_fetch_array instead of oci_fetch_all
Remove the UTF8 encoding on the connexion to oracle (I know, its stupid)
The strange thing is that I can't find any thing on the internet about this problem... It's like nobody faced the same issue. Maybe i'm doing something wrong...
Thanks in advance
You can use an extra option in json_encode:
json_encode($rows, JSON_NUMERIC_CHECK);
However this option requires a PHP version of 5.3.3 or higher (thus its ok for you).
All database extensions in PHP work like this, there's nothing you can do about it.
You'll have to manually type-cast the database results.
I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124
I'm having this problem that's driving me nuts.
I've got a MySQL database in which there is a table that contains a text field. I am querying the table in PHP and trying to put the content of the text field for each row into a var.
I am doing something like this:
for ($i=0;$i<$nbrows;$i++){
$id = $data[$i]['ID'];
$description = $data[$i]['DESCRIPTION'];
$mystring .= '<div>'.$id.': '.$description.'</div>';
}
DESCRIPTION is my text field.
I'll pass on the details. The $data array is built from mysql_fetch_array($result). I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change.
Anyway, the problem is this: if I do "echo $description;" then it works. I am getting my text field data as expected. The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. What happens in that case is it seems to be taking $description for some kind of array or object. To make the above example work, I have the replace the string with:
$mystring .= '<div>'.$id.': '.$description[0].'</div>';
So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. (it doesn't actually seem to be an array because I can't implode it).
I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do.
There is no issue with fields that aren't text.
Thanks for any ideas!
There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further.
Maybe you could post a var_dump of your $data array?
Have you tried $mystring .= "<div> $id : $description </div>";
Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. This is just because of text encoding/escaping and nothing else. I just didn't see well enough where the problem was actually happening.
Thanks for taking the time to read and respond!