【问题标题】:Want to remove Unexpected object like "headers","original","exception" from Laravel JSON output想要从 Laravel JSON 输出中删除 Unexpected 对象,如“headers”、“original”、“exception”
【发布时间】:2019-02-26 12:34:34
【问题描述】:

我收到这样的 JSON 响应。但我想删除“标题”、“原始”和“异常”。

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

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

             }
         ],
         "exception": null
   }

预期输出:

{
    "data": {
      "id": 271,
      "name": "TestController",
      "parent_id": null
    },
    "errors": [],
    "success": true,
    "status_code": 200
}

【问题讨论】:

  • 数据来自哪里。我的意思是它来自第三方 API?
  • 没有。我已经创建了 API 并希望返回响应。但是这个额外的信息正在生成。我正在使用“jwt-auth”包进行基于令牌的身份验证。

标签: json laravel


【解决方案1】:

你可以用这个

$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);

【讨论】:

  • 感谢您的回复。但是问题还没有解决。
【解决方案2】:

您可能使用response()->json() 将数据 json 返回翻倍

你只能使用数组

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

【讨论】:

    【解决方案3】:

    在我的情况下,这个问题通过这个解决方案解决了:

    你可以使用:

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

    并返回响应

    【讨论】:

      【解决方案4】:

      这就是我所做的,它对我有用: 得到您的响应后,只需调用原始对象:

      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
      

      }

      【讨论】:

        【解决方案5】:

        您正在返回一个 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() 返回到第二个

        【讨论】:

          【解决方案6】:

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

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

          // 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]);
          }
          

          【讨论】:

            猜你喜欢
            • 2013-08-31
            • 2011-02-18
            • 1970-01-01
            • 1970-01-01
            • 2018-07-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-25
            相关资源
            最近更新 更多