【问题标题】:Transform string to json in Laravel api在 Laravel api 中将字符串转换为 json
【发布时间】:2018-09-08 15:52:13
【问题描述】:

两个月前,我在 laravel 中创建了一个 api,并用 postman 对其进行了测试。一切正常。 现在我将继续开发,但我无法像以前那样访问元素。

邮递员:

主体:

{
   "RFQ" : "123",
   "client_id": "2",
   "ITEMS": [
      {
         "material" : "1.234.565",
         "description" : "Test material 1",
         "quantity" : "2.123",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "2018-01-12",
      },
      {
         "material" : "9.87564.2",
         "description" : "Test material 2",
         "quantity" : "4",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "15.01.2018"
      }
   ]
}

控制器:

public function import(ImportRequestForQuoteRequest $request, $id)
{
   return $request->getContent();
}

以前,我可以得到 $request->client_id 这样的 client_id 作为示例,但现在它什么也没返回。

如果我返回$request->getContent(),我会得到一个类似于正文的字符串。

我必须做什么才能访问这些值?

【问题讨论】:

  • dd($request->all()); 返回什么?

标签: laravel api


【解决方案1】:

尝试像这样返回它

public function import(ImportRequestForQuoteRequest $request, $id)
{
   return response()->json($request->getContent());
}

源文档: https://laravel.com/docs/5.6/responses#json-responses

【讨论】:

  • 这个例子只是为了测试我是否可以访问这些值。我喜欢访问 client_id 的值以在控制器中使用它。
  • 我的错,我想念@bmatovu 的问题,答案几乎说明了如何访问这些参数。如果这些不起作用,您可以随时通过 json_encode 运行它们
【解决方案2】:

你可以在你的控制器中试试这个......

use Illuminate\Http\Request;

public function myFunction(Request $request, $id)
{
   $post_param = $request->post_param;

   $default = '';
   $post_param = $request->input('client_id', $default);

   $route_param = $id;

    return response()->json(['params' => $request->all()]);
}

【讨论】:

  • 如果我返回return $request->client_id;,即使return $request->getContent() 显示完整内容也没有输出
  • 试试dd($request->all())看看有什么参数被传递给函数
  • dd($request->all()) 不返回任何内容
  • dd(request->getContent()) 返回正文:""" {\n "RFQ" : "123",\n "client_id": "2",\n "ITEMS": [\n {\n "material" : "1.234.565",\n "description" : "Test material 1",\n "quantity" : "2.123",\n "Quot. Deadline" : "2018-01-12",\n "delivery_date" : "2018-01-12",\n \n },\n {\n "material" : "9.87564.2",\n "description" : "Test material 2",\n "quantity" : "4",\n "Quot. Deadline" : "2018-01-12",\n "delivery_date" : "15.01.2018"\n \n }\n ]\n } """
【解决方案3】:

您需要将 json 解码为 php 数组,然后您就可以像 php 中的普通数组一样访问

$item = json_decode($request->ITEMS);

【讨论】:

  • 它返回 null
  • 所以你首先通过 dd() 函数检查数据是否来自请求
  • 没有区别。它仍然返回 null
  • 因为你的 json 字符串在这里是无效的检查主题,并且像:codebeautify.org/jsonviewer
  • 我看到字符串中有 3 次 "。但我必须从他们来的地方搜索
【解决方案4】:

你的身体:

{
   "RFQ" : "123",
   "client_id": "2",
   "ITEMS": [
      {
         "material" : "1.234.565",
         "description" : "Test material 1",
         "quantity" : "2.123",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "2018-01-12",
      },
      {
         "material" : "9.87564.2",
         "description" : "Test material 2",
         "quantity" : "4",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "15.01.2018"
      }
   ]
}

更改为:

{
   "RFQ" : "123",
   "client_id": "2",
   "ITEMS": [
      {
         "material" : "1.234.565",
         "description" : "Test material 1",
         "quantity" : "2.123",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "2018-01-12"
      },
      {
         "material" : "9.87564.2",
         "description" : "Test material 2",
         "quantity" : "4",
         "Quot. Deadline" : "2018-01-12",
         "delivery_date" : "15.01.2018"
      }
   ]
}

json 数组应该是格式正确的。所以尝试在第一项的“delivery_date”从数组中删除逗号。 您将使用 $request->all() 获得结果。

希望这能解决。

【讨论】:

  • JSON 实际上允许有尾随逗号。
【解决方案5】:

我对 phpstorm REST Client 做了同样的请求。使用相同的正文和标题,一切正常。 Postman 在内容前后加了""" 不知道为什么。 所以错误在 Postman 的任何地方

【讨论】:

    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 2018-06-28
    • 2012-10-06
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多