个人经验: 平常开发基于 python、go,完全没学过 php。
最近,有个需求,修改实现一个 lumen 项目的接口,业务逻辑基本实现了。 但是,model 的一个字段在 mysql 中是 json 类型保存,请求接口的返回消息为 json 字符串,需要把这个字段的返回消息生成标准的 json 对象。 google 了半天,没找到方法(个人完全不熟悉 php )。
求教一下大家,如何把下面的 priceinfo 字段在请求接口返回时,生成标准的 json 对象。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as BaseModel;
abstract class Model extends BaseModel
{
public function __construct(array $attributes = []) {
parent::__construct($attributes);
}
}
<?php
namespace App\Models;
class RefundFlowModel extends Model
{
protected $table = 't_refund_flow';
protected $primaryKey = 'id';
protected $hidden = array();
protected $fillable = array(
'instanceId',
'productId',
'ownerUin',
'providerOwnerUin',
'deliverType',
'flags',
'refundType',
'orderName',
'refundOrderName',
'orderType',
'priceInfo', // 此字段在 mysql 里为 json 类型
'status',
'deleted',
'isNotify',
'reasonId',
'remark',
'startTime',
'endTime',
);
protected $jsonColumns = ['priceInfo'];
{
"Response": {
"RefundOrderSet": [
{
"EndTime": "2019-01-23 10:13:09",
"InstanceId": "1111111111",
"OrderName": "222222222222",
"OrderType": "RENEW",
"OwnerUin": 3333333333,
"PriceInfo": "{\"spec\": \"规格一\", \"cycle\": \"1 个月\", \"price\": 100, \"specId\": \"97-008592-ybmbkl\", \"cycleId\": null, \"isTrial\"
: false, \"disprice\": 100, \"goodsNum\": 1, \"maxQuota\": 0, \"timeSpan\": 1, \"timeUnit\": \"m\", \"totalCost\": 100, \"trialDays\": 0, \"unitPrice\": 100, \"tradePriceId\": 0, \"realTotalCost\": 100}",
"RefundOrderName": "",
"StartTime": "2019-01-23 10:13:09",
"Status": 2
}
],
"RequestId": "",
"TotalCount": 2
}
}
求教一下大家,如何把上面的 priceinfo 字段在请求接口返回时,生成标准的 json 对象。
class DescribeRefundOrderSet extends Controller
{
public function __construct()
{
parent::__construct();
}
public function handle()
{
$rules = [
'InstanceId' => 'required|string', // 实例ID
'Limit' => 'required|integer|min:0',
'Offset' => 'required|integer|min:0',
];
if (null != ($err = $this->valid($this->request(), $rules)))
{
$this->logger->error(sprintf(
'%s::%s params check failed. reason is: %s',
__CLASS__, __METHOD__, $err));
return $err;
}
$params = $this->parameters();
$conds = [
'instanceId' => $params['InstanceId'],
];
$query = RefundFlowModel::query()
->select(['ownerUin as OwnerUin', 'instanceId as InstanceId', 'orderName as OrderName', 'refundOrderName as RefundOrderName', 'orderType as OrderType', 'status as Status', 'priceInfo as PriceInfo', 'startTime as StartTime', 'endTime as EndTime'])
->where('instanceId', $conds['instanceId']);
// 总数
$totalCount = $this->totalCount($query);
// 分页
$this ->pagination($query,
(int)$this->parameter('Offset'),
(int)$this->parameter('Limit')
);
// 查询
$refundInstanceSet = $query->get();
foreach ($refundInstanceSet as $item) {
print($item);
}
return Response::new(array(
'RefundOrderSet'=>$refundInstanceSet,
'TotalCount'=>$totalCount,
));
}
}
protected $casts = [
'priceInfo' => 'array',
];
public function getPriceInfoAttribute($value)
{
return json_decode($value);
}
$refundInstanceSet = $query->get();
//$refundInstanceSet['PriceInfo']=json_decode($refundInstanceSet['PriceInfo'],true);
//$refundInstanceSet['priceInfo']=json_decode($refundInstanceSet['priceInfo'],true);
//$refundInstanceSet[PriceInfo]=json_decode($refundInstanceSet[PriceInfo],true);
以上方法都没成功
目前,逻辑上添加了这段代码, 基本解决问题
foreach ($refundInstanceSet as $item) {
#$item['PriceInfo'] = json_decode($item['PriceInfo']);
$item['PriceInfo'] = json_decode(json_encode($item['PriceInfo'], true));
}
1
FaceBug 2020-01-18 16:59:59 +08:00
// 查询
$refundInstanceSet = $query->get(); $refundInstanceSet[PriceInfo] = json_decode($refundInstanceSet[PriceInfo] ,true); |