解析将JSON Webhook解析为CSV

时间:2014-12-30 17:18:01

标签: json parsing csv shopify

我需要将已经以JSON格式导出的Shopify Webhook中的特定字段解析为CSV文件。我需要定位的其中一个字段是送货地址,如何选择“shipping_address”==> “地址1”:“123航运街”?

{
"created_at":"2014-12-30T11:58:01-05:00",
"email":"jon@doe.ca",
"name":"#9999",
"subtotal_price":"229.94",
"taxes_included":false,
"order_number":1234,
"billing_address":{
    "address1":"123 Billing Street",
    "address2":null,
    "city":"Billtown",
    "company":"My Company",
    "country":"United States",
    "first_name":"Bob",
    "last_name":"Biller",
    "latitude":null,
    "longitude":null,
    "phone":"555-555-BILL",
    "province":"Kentucky",
    "zip":"K2P0B0",
    "name":"Bob Biller",
    "country_code":"US",
    "province_code":"KY"
    },
"shipping_address":{
    "address1":"123 Shipping Street",<!-- NEED THIS ADDRESS -->
    "address2":null,
    "city":"Shippington",
    "company":"Shipping Company",
    "country":"United States",
    "first_name":"Steve",
    "last_name":"Shipper",
    "latitude":null,
    "longitude":null,
    "phone":"555-555-SHIP",
    "province":"Kentucky",
    "zip":"K2P0S0",
    "name":"Steve Shipper",
    "country_code":"US",
    "province_code":"KY"
},
"customer":{
    "accepts_marketing":false,
    "created_at":null,
    "email":"john@test.com",
    "first_name":"John",
    "id":null,
    "last_name":"Smith",
    "last_order_id":null,
    "multipass_identifier":null,
    "note":null,
    "orders_count":0,
    "state":"disabled",
    "total_spent":"0.00",
    "updated_at":null,
    "verified_email":true,
    "tags":"",
    "last_order_name":null,
    "default_address":{
        "address1":"123 Elm St.",
        "address2":null,
        "city":"Ottawa",
        "company":null,
        "country":"Canada",
        "first_name":null,
        "id":null,
        "last_name":null,
        "phone":"123-123-1234",
        "province":"Ontario",
        "zip":"K2H7A8",
        "name":"",
        "province_code":"ON",
        "country_code":"CA",
        "country_name":"Canada",
        "default":true
    }
}

2 个答案:

答案 0 :(得分:1)

这是从webhook获取数据json的一种方法:

class Data{
    private $arrayOrder = array();
    public function __construct(){
        $this->process();
    }

    private function process(){
        $order = $this->getContents();
        if(!$order){
            error_log('something failed');
            exit();
        }
        $this->arrayOrder = array(
            'email'=>$order->email,
            'billing_address_address1'=>$order->billing_address->address1,
            'shipping_address_address1'=>$order->shipping_address->address1,
            'customer'=>array(
                'first_name'=>$order->customer->first_name,
                'default_address'=>$order->customer->default_address->address1
            )
        );
        error_log(json_encode($this->arrayOrder));//to test
    }

    private function getContents(){
        $webhookContent = "";
        $webhookContent = file_get_contents("php://input");
        if($webhookContent){
            $data = json_decode($webhookContent);
            return $data;
        }
        return false;
    }
}
$csv = new Data();

创建csv,使用手动示例来放置csv:http://php.net/manual/en/function.fputcsv.php

答案 1 :(得分:0)

当然使用JSON解析器。最明显的答案始终是我选择的那个。由于JSON可以使用Javascript直接解释,因此您选择的脚本语言可能是Javascript来执行此操作!如果由于某种原因你不能用Javascript编写你的脚本代码,几乎所有其他脚本语言都带有JSON解析指令。例如,在Ruby中,您只需执行

data = JSON.parse someJSON

现在您可以使用数据。通过不告诉全世界您在问题中选择脚本语言,或者您是在客户端还是服务器世界,几乎不可能帮助您更多。幸运的是,JSON和CSV文件一样只是文本。因此,没有什么能阻止您使用JSON解析器或只是简单的旧Javascript来解析您的JSON。