开发者

submit a form in json format and update json file

开发者 https://www.devze.com 2023-03-23 00:40 出处:网络
Following is the json file for customers: https://we4cs.myshopify.com/admin/customers.json I have a html form that should get submitted and update the json file. How do i do it? Please advise.

Following is the json file for customers: https://we4cs.myshopify.com/admin/customers.json

I have a html form that should get submitted and update the json file. How do i do it? Please advise.

Following is the json file that is customers.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,"or开发者_JAVA技巧ders_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"}]}


  1. Pick a server side programming language. PHP is popular in the budget world. ASP.NET is popular in the Microsoft world. Java is popular in the Enterprise world. I like Perl. Other options are available.
  2. Parse the incoming data. There are libraries that do this for almost any programming language you can name.
  3. Apply a file lock to the JSON file
  4. Parse the JSON using a library for whatever language you are using.
  5. Add the new data to the memory structure you've created
  6. Save the file
  7. Remove the lock
  8. Return an OK message to the client

That said, you would probably be better off generating the JSON on demand and using it only as a transport mechanism. A real database is almost always a better option for storage and manipulation.


Basic way of writing to a file, you need fopen, fwrite and of course, 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');
    }
}

?>

EDIT: Now that you've mentioned it's from an API, you cannot use this to write to an external server.

0

精彩评论

暂无评论...
验证码 换一张
取 消