以json格式提交表单并更新json文件

时间:2011-07-27 11:41:22

标签: jquery json

以下是客户的json文件: https://we4cs.myshopify.com/admin/customers.json

我有一个应该提交的html表单并更新json文件。我该怎么做?请指教。

以下是customers.json

的json文件
{"customers":[{"accepts_marketing":false,"orders_count":0,"addresses":[{"company":"test","city":"test","address1":"test","name":"test
test","zip":"56576","address2":"test","country_code":"AZ","country":"Azerbaijan","province_code":null,"phone":"54765765878","last_name":"test","province":"hgjghj","first_name":"test"}],"tags":"","id":51036842,"last_name":"test","note":null,"email":"test@test.com","first_name":"test","total_spent":"0.00"},{"accepts_marketing":false,"orders_count":0,"addresses":[{"company":"","city":"newark","address1":"23
smith","name":"gggggggggggggggg
hhhhhhhhhhhhhhhhhh","zip":"08786","address2":"","country_code":"US","country":"United
States","province_code":"NJ","phone":"","last_name":"hhhhhhhhhhhhhhhhhh","province":"New
Jersey","first_name":"gggggggggggggggg"}],"tags":"","id":49755872,"last_name":"hhhhhhhhhhhhhhhhhh","note":null,"email":"gregadkins2001@aol.com","first_name":"gggggggggggggggg","total_spent":"0.00"},{"accepts_marketing":false,"orders_count":0,"addresses":[{"company":"dsfsdf","city":"newark","address1":"12
dfsdf","name":"sdfsdf
sdfsdf","zip":"08876","address2":"","country_code":"US","country":"United
States","province_code":"NJ","phone":"","last_name":"sdfsdf","province":"New
Jersey","first_name":"sdfsdf"}],"tags":"","id":47565872,"last_name":"sdfsdf","note":null,"email":"sdfdfsdaf@aol.com","first_name":"sdfsdf","total_spent":"0.00"},{"accepts_marketing":true,"orders_count":0,"addresses":[{"company":"xcvxcvcx","city":"bridgewater","address1":"112
asdasd","name":"zxcxzcxz
cxvxcvxv","zip":"08875","address2":"asdasd","country_code":"US","country":"United
States","province_code":null,"phone":"323123123","last_name":"cxvxcvxv","province":"","first_name":"zxcxzcxz"},{"company":"xcvxcvcx","city":"bridgewater","address1":"112
asdasd","name":"zxcxzcxz
cxvxcvxv","zip":"08875","address2":"asdasd","country_code":"US","country":"United
States","province_code":"NJ","phone":"323123123","last_name":"cxvxcvxv","province":"New
Jersey","first_name":"zxcxzcxz"}],"tags":"","id":40799732,"last_name":"cxvxcvxv","note":"","email":"handful4me@aol.com","first_name":"zxcxzcxz","total_spent":"0.00"}]}

2 个答案:

答案 0 :(得分:0)

  1. 选择服务器端编程语言。 PHP在预算领域很受欢迎。 ASP.NET在Microsoft世界中很受欢迎。 Java在企业界很受欢迎。我喜欢Perl。还有其他选择。
  2. 解析传入的数据。几乎所有可以命名的编程语言都有这样的库。
  3. 将文件锁应用于JSON文件
  4. 使用您正在使用的任何语言的库解析JSON。
  5. 将新数据添加到您创建的内存结构
  6. 保存文件
  7. 取消锁定
  8. 向客户端返回OK消息
  9. 也就是说,您可能最好根据需要生成JSON并仅将其用作传输机制。真正的数据库几乎总是存储和操作的更好选择。

答案 1 :(得分:0)

写入文件的基本方式,您需要fopenfwrite,当然还需要fclose

<?php

if($_POST['json'])
{
    $open_json = fopen('json_file.json', 'w');

    if($open_json)
    {
        if(fwrite($open_json, $_POST['json'] === FALSE)
        {
            die('failed to write to file');
        }
        fclose($open_json);
    }
    else
    {
        die('failed to open file');
    }
}

?>

编辑:既然您已经从API中提到过它,就不能使用它来写入外部服务器。