我正在从bigCommerce
api创建以下对象,如下所示:
[
{
"id": 412,
"option_id": 37,
"display_name": "testSteveMemory",
"sort_order": 0,
"is_required": true
},
{
"id": 413,
"option_id": 34,
"display_name": "Hard Drive (desktop)",
"sort_order": 1,
"is_required": true
},
{
"id": 414,
"option_id": 24,
"display_name": "Include Keyboard & Mouse",
"sort_order": 2,
"is_required": true
},
{
"id": 415,
"option_id": 33,
"display_name": "Memory",
"sort_order": 3,
"is_required": true
}
]
我使用以下方法将其转换为PHP数组:
$curlProductOptions = json_decode($curlProductOptions, true);
然后我遍历数组并获取该option_id的选项
$allOptions = array();
foreach($curlProductOptions as $value){
//echo $value['option_id'].'<br>';
$option_id = $value['option_id'];
$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);
curl_close($curl);
}
然后我使用echo输出数组$ allOptions,我在移动应用程序中将其作为json使用。 如果我使用
echo $curlProductData.'<br>'
我得到以下内容:
[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}]
[{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}]
[{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}]
[{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]
如果我使用以下任何一种回声:
echo $allOptions;
json_encode($allOptions);
var_dump($allOptions);
将数据返回到移动应用程序时出现解析错误。即它没有将结果识别为json数组。
我在假设:
$allOptions = array()
将$allOptions
声明为数组
我在循环中为每个$allOptions
填充$curlProductData
。
我在这里做错了什么?
我现在正在使用:
echo "[".implode(",\n",$allOptions)."]";
我现在收到以下内容:
[[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}], [{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}], [{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}], [{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]]
但我无法正确映射?
MrWarby
答案 0 :(得分:1)
您正在逐个输出多个数组。您一次只能拥有一个JSON对象。
最好是坚持使用数组并使用echo json_encode($curlProductData)
输出数据。
答案 1 :(得分:1)
以下解决方案将每个项目放在一个数组中:
foreach($curlProductOptions as $value){
//echo $value['option_id'].'<br>';
$option_id = $value['option_id'];
$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
foreach(json_decode($curlProductData, true) as $key => $value) {
$allOptions[] = $value; // [] is quicker than array_push() for single items
}
curl_close($curl);
}
如果您在问题中需要四个分组:
以下解决方案将每个项目放在一个数组中:
foreach($curlProductOptions as $value){
//echo $value['option_id'].'<br>';
$option_id = $value['option_id'];
$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
$allOptions[] = json_decode($curlProductData, true);
curl_close($curl);
}
现在json_encode($allOptions)
将为您提供有效的json字符串。问题是当你只读1时,你试图传递4个json字符串。
答案 2 :(得分:0)
如果您收到解析器错误,可能是因为echo $curlProductData.'<br>'
之后缺少分号
答案 3 :(得分:0)
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);
问题是curl_exec
正在返回一个字符串 - 该字符串可能是一个JSON字符串,但它仍然是一个字符串。
您的最终echo json_encode($allOptions)
将是一个字符串数组,而不是您可能期望的多维数组。
你可以尝试的是echo "[".implode(",\n",$allOptions)."]";
,因为这会产生一个多维数组。
但请记住,必须在循环之后完成此操作。如果你在循环中仍然这样做的话,你最终会得到多个输出,这会导致问题。