想要从Laravel JSON输出中删除意外对象,例如“标头”,“原始”,“异常”

时间:2018-09-22 08:32:09

标签: json laravel

我收到这样的JSON响应。但我想删除“标题”,“原始”和“例外”。

public makeSubmitAPI(oncallData): Observable<any> {
        let orderPayload = this.postAPIService.prepareOrderPayload(oncallData);
        let url = "";
        let orderResponse: any;
        return this.apiCallsService.apiCall('placeOrder', orderPayload, 'post', false)
            .map((orderRes: any) => {
                orderResponse = orderRes;
                url = `orders/${orderResponse.data.id}/progress`;
                let progressPayload = this.postAPIService.prepareProgressAPIData(oncallData, orderResponse.data, this.userType);
                return progressPayload;
            }).pipe(throttle(val=> interval(5000)))
            .flatMap(progressPayload => {
                    return this.apiCallsService.apiCall(url, progressPayload, 'post', false).pipe(throttle(val=> interval(5000)))
            })
            .flatMap(progressResponse => {
                return Observable.combineLatest(
                    orderResponse.data.serviceAddresses.map((address, index) => {
                        let fullfilledAPI = this.postAPIService.prepareFullfilledAPIData(oncallData, orderResponse.data, progressResponse, this.userType, index, orderPayload);
                        return this.apiCallsService.apiCall('fullfillment', fullfilledAPI, 'post', false).map(res => res);
                    })
                )
            });
    }

预期输出:

    {
         "headers": {},
         "original": [        

             {
                 "id": 271,
                 "name": "TestController",
                 "parent_id": null

             }
         ],
         "exception": null
   }

6 个答案:

答案 0 :(得分:0)

您可以使用此

$json='{
     "headers": {},
     "original": [        

         {
             "id": 271,
             "name": "TestController",
             "parent_id": null

         }
     ],
     "exception": null
}';

$arr=json_decode($json);
$data=$arr->original[0];

$new_json=array();
$new_json['data']=$data;
$new_json['errors']=[];
$new_json['success']=true;
$new_json['status_code']=200;

$new_json=json_encode($new_json);

答案 1 :(得分:0)

您可能已将response()->json()的数据json返回值加倍了

您只能使用数组

return ["data"=> [
  "id"=> 271,
  "name"=> "TestController",
  "parent_id"=> null
],
"errors"=> [],
"success"=> true,
"status_code"=> 200
];

答案 2 :(得分:0)

就我而言,此问题通过以下解决方案得以解决:

您可以使用:

return json_decode(json_encode($ResponseData), true);

并返回响应

答案 3 :(得分:0)

这是我所做的,并且对我有用: 在收到这样的响应后,只需调用原始对象即可:

public function user_object(){

  return $this->me()->original;
  
}

这是返回用户详细信息的me()函数

public function me()
{
    return response()->json(auth('users')->user()->only(['id','name','email','status','phonenumber','type']));
}

这是我邮递员的回复:

{
"success": true,
"user": {
    "id": 29,
    "name": "test6",
    "email": "test6@gmail.com",
    "status": 1,
    "phonenumber": "413678675",
    "type": "user"
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4wLjE6ODAwMFwvYXBpXC9hdXRoXC9yZWdpc3RlciIsImlhdCI6MTU5OTQ3MDc3OCwiZXhwIjoxNTk5NDc0Mzc4LCJuYmYiOjE1OTk0NzA3NzgsImp0aSI6InFyUWEyTVNLVzR4a2o0ZVgiLCJzdWIiOjI5LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.SMHgYkz4B4BSn-fvUqJGfsgqHc_r0kMDqK1-y9-wLZI",
"expires_in": 3600

}

答案 4 :(得分:0)

您正在返回一个 response()->json() 内的另一个 response()->json() 东西:

response()->json(response()->json($data,200),200)

或者更像:

$data = [
  "id"=> 271,
  "name"=> "TestController",
  "parent_id"=> null
];

$response = response()->json($data,200);

return response()->json($response ,200);

您可能没有注意到它,因为函数将第一个 response()->json() 返回到第二个

答案 5 :(得分:0)

触发问题是因为您在代码中的某处返回嵌套响应。

这是一个简单的代码,用于演示问题和修复。

// A normal function that you think it returns an array
// but instead, it is returning a response object!
public function get_data(){

    //ISSUE
    return response([1, 2, 3]); // <- this will trigger the issue becuase
                                // it returns the data as a response not an array

    //FIX
    return [1, 2, 3]; // <- this will work as intended 
                      // bacause the data is returned as a normal array
}

public function get_all_data(){
    $first_array = [1, 2];
    $second_array = [2, 3];
    $third_array = get_data(); // <- here is the call to the function
                               // that should return an array
    
    //Return the JSON response
    return response([first_array, second_array, third_array]);
}