【问题标题】:Remove unwanted array from json从 json 中删除不需要的数组
【发布时间】:2019-05-19 12:50:37
【问题描述】:

我是 Laravel 的新手。我正在使用函数创建一个 json 结构。这是我当前的输出:

{
    "success": "1",
    "data": [
        {
            "category_type_id": 1,
            "category_type": "Study",
            "category_icon": "http://192.168.1.132:8000/images/category/study.svg"
        },
        {
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg"
        },
        {
            "category_type_id": 3,
            "category_type": "Other",
            "category_icon": "http://192.168.1.132:8000/images/category/other.svg"
        }
    ]
}

这是我的控制器代码:

$get_all_category = CategoryType::all();

return response()->json(['success' => '1', 'data' => $get_all_category]);

我想要没有从数据开始的数组的结果请需要解决方案

【问题讨论】:

  • 请更新您的问题以显示预期结果。我不明白您所说的“没有从数据开始的数组的结果”是什么意思。
  • 先生,我只想从我的 json 中删除 [ ] 符号并在我的 json 中添加 { }
  • 你能在这里给出预期的输出吗?你需要编辑你的帖子@smita patil

标签: php json laravel laravel-5 eloquent


【解决方案1】:

只需从您的 json 响应中删除其他属性:

$get_all_category = CategoryType::all();
return response()->json($get_all_category);

这将像这样返回您的 json:

[ 
    { 
        "category_type_id": 1,
        "category_type": "Study",
        "category_icon": "http://192.168.1.132:8000/images/category/study.svg"
    },
    {
        "category_type_id": 2,
        "category_type": "Sports",
        "category_icon": "http://192.168.1.132:8000/images/category/game.svg" },
    {
        "category_type_id": 3
        "category_type": "Other",
        "category_icon": "http://192.168.1.132:8000/images/category/other.svg"
     }
]

如果你想保持成功属性,你可以这样做,但它只会将你想要摆脱的旧数据属性更改为“0”:

$get_all_category = CategoryType::all();
return response()->json(['success' =>'1', $get_all_category]);

【讨论】:

  • 我仍然强烈建议您像以前一样发送您的 json,以便使用您的 json 响应的应用程序知道从哪里获取数据(数据属性)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2020-06-25
  • 2013-04-26
  • 2016-04-17
  • 2013-04-17
  • 2021-06-27
相关资源
最近更新 更多