使用javascript和php将json对象写入.json文件

时间:2022-07-20 15:25:18

I need to write a json object to a .json file using javascript and php. The json object seems to be storing the values correctly, but the php file doesn't seem to run so nothing happens.

我需要使用javascript和php将json对象写入.json文件。json对象似乎正确地存储了值,但是php文件似乎没有运行,所以什么也没有发生。

Thanks.

谢谢。

my javascript function, which is called on a button click:

我的javascript函数,按下按钮即可调用:

    function save_crime()
{
    //json object, v1, v2, v3 etc are variables whose values are set in a different function
    var jsonObject = { "crime" : {  "violence" : {  "violence1" : v1,
                                                    "violence2" : v2,
                                                    "violence3" : v3
                                    },

                                    "burglary" : {  "burglary1" : b1,
                                                    "burglary2" : b2
                                    },

                                    "robbery" :  {  "robbery1" : r1,
                                                    "robbery2" : r2
                                    },

                                    "criminal" : {  "criminal1" : c1,
                                                    "criminal2" : c2,
                                                "criminal3" : c3
                                    } 
                                } 
                    }



    //jQuery to post json object to json.php for writing to json file
    $.ajax({
        type : "POST",
        url : 'json.php',
        dataType : 'json', 
        data : {
            json : JSON.stringify(jsonObject)
        }
    });

}

php file for writing to .json file:

用于写入.json文件的php文件:

<?php

$json = $_POST['json'];
$info = json_encode($json);

$file = "crimes.json";
$handle = fopen($file, 'w');
fwrite($handle, $info);
fclose($handle);

?>

1 个解决方案

#1


1  

You are writing the JSON string to a file, you dont need to decode as its a string already from JS part (JSON.stringify(jsonObject)).

您正在将JSON字符串写入文件中,不需要将其解码为已经来自JS部分的字符串(JSON.stringify(jsonObject))。

Just write it directly.

直接写它。

file_put_contents("crimes.json",  $_POST['json']);

Its better to get a response from server so that you know the action is performed properly. Use the $.post shorthand instead of $.ajax.

最好从服务器获得响应,这样您就知道正确地执行了操作。使用美元。使用简写而不是$.ajax。

$.post(
    'json.php',
    {
        json : JSON.stringify(jsonObject)
    },
    function (data, textStatus, jqXHR){
    }
);

#1


1  

You are writing the JSON string to a file, you dont need to decode as its a string already from JS part (JSON.stringify(jsonObject)).

您正在将JSON字符串写入文件中,不需要将其解码为已经来自JS部分的字符串(JSON.stringify(jsonObject))。

Just write it directly.

直接写它。

file_put_contents("crimes.json",  $_POST['json']);

Its better to get a response from server so that you know the action is performed properly. Use the $.post shorthand instead of $.ajax.

最好从服务器获得响应,这样您就知道正确地执行了操作。使用美元。使用简写而不是$.ajax。

$.post(
    'json.php',
    {
        json : JSON.stringify(jsonObject)
    },
    function (data, textStatus, jqXHR){
    }
);