我从woocommerce商店导出订单项,需要通过JSON格式的API发送数据。我有一个数组用于这个JSON示例的简单部分,但是由于我不熟悉JSON对象以及如何将其构建为数组,因此我在此部分丢失了。
示例JSON:
"bundles": [{
"type": "TYPE1",
"items": [{
"bom": [{
"type": "C",
"stockId": "1",
"quantity": 1,
"metadata": {
"key": "value"
}
},
{
"type": "E",
"quantity": 1,
"stockId": "2"
}
]
}],
"delivery": {
"method": "FIRST_CLASS_ROYAL_MAIL",
"shippingAddress": {
"firstname": "value",
"lastname": "value",
"addressLine1": "value",
"addressLine2": "value",
"town": "value",
"county": "value",
"postcode": "value"
}
}
},
{
"type": "TYPE2",
"items": [{
"bom": [{
"type": "C",
"stockId": "4",
"quantity": 1,
"metadata": {
"key": "value",
"key": "value"
}
}
]
}],
"delivery": {
"method": "E",
"key": "value",
"key": "value"
}
}
]
到目前为止,我目前的PHP是同一个对象的另一部分:
$postData = array(
's' => array(
'n' => 'T',
'k' => '5'
),
'c' => array(
'n' => 'C',
'e' => 'c'
),
'p' => array(
'p' => 'S',
't' => 't',
'a' => '8',
'c' => '8'
)
);
以上内容是作为标准的php数组构建的,在进行JSON编码时它会正确格式化吗?
答案 0 :(得分:0)
我知道的唯一方法是json编码这样的数组:
<?php
echo json_encode(
[
"bundles" =>[ [
"type" => "TYPE1",
"items" => [
[
"bom" => [
[
"type" => "C",
"stockId" => "1",
"quantity" => 1,
"metadata" => [
"key" => "value"
]
],
[
"type" => "E",
"quantity" => 1,
"stockId" => "2"
]
]
]
],
"delivery" => [
"method" => "FIRST_CLASS_ROYAL_MAIL",
"shippingAddress" => [
"firstname" => "value",
"lastname" => "value",
"addressLine1" => "value",
"addressLine2" => "value",
"town" => "value",
"county" => "value",
"postcode" => "value"
]
]
],
[
"type" => "TYPE2",
"items" => [
[
"bom" => [
[
"type" => "C",
"stockId" => "4",
"quantity" => 1,
"metadata" => [
"key" => "value",
"key" => "value"
]
]
]
]],
"delivery" => [
"method" => "E",
"key" => "value",
"key" => "value"
]
]]
]
);
返回
{ “束”:[{ “类型”: “TYPE1”, “项目”:[{ “BOM”:[{ “类型”: “C”, “stockId”: “1”, “数量”: 1, “元数据”:{ “键”: “值”}},{ “类型”: “E”, “数量”:1, “stockId”: “2”}]}], “交货”:{”方法 “:” FIRST_CLASS_ROYAL_MAIL “ ”shippingAddress“:{ ”姓“: ”值“, ”姓“: ”值“, ”addressLine1“: ”值“, ”addressLine2“: ”值“, ”镇“:” 值”, “郡”: “值”, “邮政编码”: “值”}}},{ “类型”: “TYPE2”, “项目”:[{ “BOM”:[{ “类型”: “C”, “stockId”: “4”, “数量”:1, “元数据”:{ “键”: “值”}}]}], “交货”:{ “方法”: “E”, “关键”:”值“}}]}
这正是你的json ......
答案 1 :(得分:0)
我有一个例子,您可以查看此问题以解决您的问题代码,如下所示。
$ jsonArray = json_decode(的file_get_contents(&#34; PHP://输入&#34)); if(!empty($ jsonArray)){
foreach ($jsonArray->workers as $key => $value)
{
$workers_ins="insert into workers_mst (workers_name,date,apply_id,active_flag,crt_date) values ('".$value->workers_name."','".$value->date."','".$apply_id."','1','".time()."')";
mysqli_query($con,$workers_ins) or throw_error(mysqli_error($con));
}
}