3

我一直在努力使用以下代码使其可用,但我不知道它是什么类型的数据。

这部分是从 ERPNext 发布到我的 webhook 的有效负载中提取的。我已经深入研究了数据的特定部分,但它似乎混合了不同的数据类型。

Array
(
    [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
        (
            [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
        )

)

以下是来自有效载荷的原始 json 解码数据

Array
(
    [data] => 
    [{"items": ] => Array
        (
            [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
                (
                    [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
                )

        )

    [headers] => Array
        (
        )

)

我期待着像这样干净的东西

Array
(
    [stock_qty] => 131.0
    [base_price_list_rate] => 4500.0
    [creation] => 2019-04-02 22:33:56.356904
    [base_amount] => 589500.0
)
4

2 回答 2

2

这有点奇怪,您必须将 json 拼接在一起(在键和值中)

尝试以下函数,它遍历解码后的数组,然后检测数据的开始,{将 json 拼接在一起,并再次通过 json decode 传递它。

function decodeweird($data,$sub=false) {
    $return = '';
    foreach($data as $key => $value) {
        //detect the beginning of the data, or just append if we've already started
        if($sub || substr($key,0,1) == '{') {
            $return .= $key;
            if(!empty($value) && is_array($value)) $return .= decodeweird($value,true);
        }
    }
    return $sub ? $return:json_decode($return,true);
}

$received_data = <<<EOD
{
  "{\"items\": ": {
    "{\"stock_qty\": 131.0, \"base_price_list_rate\": 4500.0, \"image\": \"\", \"creation\": \"2019-04-02 22:33:56.356904\", \"base_amount\": 589500.0, \"qty\": 131.0, \"margin_rate_or_amount\": 0.0, \"rate\": 4500.0, \"total_weight\": 0.0, \"ensure_delivery_based_on_produced_serial_no\": 0, \"owner\": \"Administrator\", \"target_warehouse\": null, \"stock_uom\": \"Nos\", \"base_net_amount\": 589500.0, \"page_break\": 0, \"modified_by\": \"Administrator\", \"base_net_rate\": 4500.0, \"discount_percentage\": 0.0, \"item_name\": \"5277\", \"amount\": 589500.0, \"actual_qty\": 0.0, \"net_rate\": 4500.0, \"conversion_factor\": 1.0, \"base_rate_with_margin\": 0.0, \"supplier\": null, \"docstatus\": 1, \"prevdoc_docname\": null, \"uom\": \"Nos\", \"ordered_qty\": 0.0, \"doctype\": \"Sales Order Item\", \"description\": \"5277\", \"parent\": \"SAL-ORD-2019-00019\", \"gross_profit\": 589500.0, \"returned_qty\": 0.0, \"brand\": null, \"base_rate\": 4500.0, \"item_code\": \"5277\", \"produced_qty\": 0.0, \"projected_qty\": -453.0, \"warehouse\": \"Stores - AFSPK\", \"margin_type\": \"\", \"billed_amt\": 0.0, \"rate_with_margin\": 0.0, \"pricing_rule\": null, \"delivered_qty\": 0.0, \"delivered_by_supplier\": 0, \"discount_amount\": 0.0, \"price_list_rate\": 4500.0, \"weight_uom\": null, \"transaction_date\": \"2019-04-02\", \"name\": \"f94099637a\", \"idx\": 1, \"item_tax_rate\": \"{}\", \"item_group\": \"Products\", \"planned_qty\": 0.0, \"modified\": \"2019-04-02 22:34:00.282021\", \"weight_per_unit\": 0.0, \"work_order_qty\": 0.0, \"parenttype\": \"Sales Order\", \"customer_item_code\": null, \"blanket_order_rate\": 0.0, \"valuation_rate\": 0.0, \"net_amount\": 589500.0, \"blanket_order\": null, \"delivery_date\": \"2019-06-28\", \"parentfield\": \"items\"}": {
      ", \"customer_address\": null, \"customer_name\": \"Muhammad\", \"name\": \"SAL-ORD-2019-00019\", \"title\": \"Muhammad\"}": null
    }
  },
  "headers": {
  }
}
EOD;
$received = json_decode($received_data,true);

var_dump(decodeweird($received));
于 2019-04-02T22:03:42.237 回答
0

看起来你有 json 数组,只需将它传递给 json_decode。

前任:

$jsonData = "{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}"

$assosArray = json_decode($jsonData, true);

注意 true,它必须设置为 true 才能成为关联数组!

于 2019-04-02T21:01:46.323 回答