wigglesby Posted January 4, 2010 Share Posted January 4, 2010 Hi all I'm trying to generate JSON, by pulling data from my database and outputting it to a file to be used later. I've managed to get all that working, the only problem is that If there is anything like "example-text" (double quotes) the JSON output is \\\"example-text\\\". I want to removed the slashes as it seems to be affecting the output of the actual paragraph of text. Everything after the slashes is not displayed (I'm pulling the JSON into flash) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/ Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 How exactly are you creating the json? Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988078 Share on other sites More sharing options...
wigglesby Posted January 4, 2010 Author Share Posted January 4, 2010 I'm using a class: <?php class mysql_to_json { var $json; var $cbfunc; var $json_array; //constructor function mysql_to_json($query = '', $cbfunc = '') { //set cbfunc $this->set_cbfunc($cbfunc); //check they don't just want a new class if($query != '') { //set query $this->set_query($query); } } //produces json output function get_json() { //generate json $this->json = $this->cbfunc . '(' . json_encode($this->json_array) . ')'; //return json return $this->json; } //produces json from query function get_json_from_query($query, $cbfunc = '') { //set cbfunc $this->set_cbfunc($cbfunc); //set query $this->set_query($query); //return json data return $this->get_json(); } //set query function set_query($query) { //execute query $exec_query = mysql_query($query); //reset json array $this->json_array = array(); //loop through rows while($row = mysql_fetch_assoc($exec_query)) { //add row array_push($this->json_array, $row); } //enable method chaining return $this; } } //set cbfunc function set_cbfunc($cbfunc) { //set cbfunc $this->cbfunc = $cbfunc; //enable method chaining return $this; } } I'm including this in another file then just creating an object and using methods from the class: //create a new instance of mysql_to_json $mtj = new mysql_to_json($query); //show the json output $json_out = rtrim(str_replace('\r\n', '',$mtj->get_json())); //create a new blank instance of mysql_to_json $mtj = new mysql_to_json(); //show the json output through method chain $fh = fopen('objects.json', 'w') or die("can't open file"); fwrite($fh, $json_out); fclose($fh); Objects.json is populated, but everything with a "(double quote) or ' (single quote) becomes \\\"example\\\" or shouldn\\'t. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988081 Share on other sites More sharing options...
wigglesby Posted January 5, 2010 Author Share Posted January 5, 2010 Can anyone help me with this? It's causing a problem parsing the JSON as it cannot parse the JSON due to the \\ chars UPDATE: I've added stripslashes to the way the json is generated. Fingers crossed no more errors on parsing. Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988736 Share on other sites More sharing options...
salathe Posted January 5, 2010 Share Posted January 5, 2010 Are you sure the problem doesn't lie with the data in the database rather than with the JSON encoding? Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988823 Share on other sites More sharing options...
wigglesby Posted January 5, 2010 Author Share Posted January 5, 2010 That is quite possible. The insert of the data uses mysql_real_escape_string. Could this be a reason why, when inserting a string, such as "example-code" then outputs "\\\example-code\\\" ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988829 Share on other sites More sharing options...
salathe Posted January 5, 2010 Share Posted January 5, 2010 Maybe, have a look at the data in the database and see if there are extraeneous slashes. Quote Link to comment https://forums.phpfreaks.com/topic/187100-json-double-quotations/#findComment-988832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.