我有一个REST API,应该将JSON的内容保存到MySQL数据库中。但是它将所有内容都插入为null?这很奇怪,因为无论何时从swift SDK发送JSON都可以正常工作。
public function saveTracking($request, $response, $args) {
if(!$this->validToken($response)) {
return $response;
}
$data = $request->getParsedBody();
$tracking_data = json_decode(json_encode($data['tracking']), FALSE);
$total_records = 0;
//error_log(print_r($tracking_data, true));
foreach($tracking_data as $key => $value) {
$tracking = new EstablishmentTracking();
$tracking->mobile_user_id = $this->authUser->id;
$tracking->report_date = $value->report_date;
$tracking->establishment_id = $value->establishment_id;
if ($value->platform == MobileUser::ANDROID || $value->platform == MobileUser::IOS) {
$tracking->platform = $value->platform;
}
$tracking->purchase_id = $value->purchase_id;
error_log(print_r($tracking, true));
$this->establishmentService->saveEstablishmentTracking($tracking);
$total_records++;
}
$arr = array("status" => "ok"
,"total_records" => $total_records
);
print_r($arr);
$newResponse = $response->withJson($arr);
return $newResponse;
}
添加一行打印变量内容后,我得到以下内容:
stdClass Object
(
[establishment_id] => 39
[report_date] => 2018-05-31 18:45:05
[platform] => android
[purchase_id] => 0
是不是正确地将JSON作为数组发送?什么似乎是问题?
答案 0 :(得分:0)
我假设json对象导致问题是$ tracking_data,你正在使用的json解码需要从false到true的第二个值,这会将它转换为php数组,它可以用键重复stdClass可能不会显示其属性< / p>