Ali
Ali

Reputation: 1678

JSON String format

I'm saving JSON string (using javascript's JSON.stringify) via Ajax call to a php script that write this string into a file i.e.

<?php

    $msg = $_GET['hecdata'];

    if (strlen($msg) > 0)
    {
        $file = fopen("hecdata.txt","w");
        fwrite($file,$msg);
        fclose($file);

        echo "True";        
    }
    else
    {
        echo "False";               
    }

?>

but the problem is the format of JSON become:

[{\"customerName\":\"Customer 1\",\"contactNumber\":\"03001234567\",\"hallName\":\"4\",\"bookingDate\":\"09/30/2013\"}]

which is not correct, I am new to php, is this how we should save a json string into a text file?

Upvotes: 0

Views: 420

Answers (2)

bacho
bacho

Reputation: 1

JSON is correct, you have extra brace "}" on this string

Upvotes: 0

Rajesh
Rajesh

Reputation: 3778

try below code:

$msg = stripslashes($msg);

and then write it to file. Refer http://php.net/manual/en/function.stripslashes.php

Upvotes: 1

Related Questions