Jump to content

JSON double quotations


wigglesby

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.