首页 > 解决方案 > JSON对象数组逗号后没有空格

问题描述

这是我的 json 数组:已更新

    {
  "Title": "Limitless",
  "Year": "2011",
  "Rated": "PG-13",
  "Released": "2011-03-18",
  "Runtime": 105,

  "Actors": [
    "Bradley Cooper",
    "Robert De Niro",
    "Abbie Cornish",
    "Andrew Howard"
  ]

}

这是我的 php 返回:

return response()->json($movie);

jQuery:

$("#stars").val(data.Actors); //Nothing fancy about this. Just outputting data to an input field.

结果:

Bradley Cooper,Robert De Niro,Abbie Cornish,Andrew Howard

为什么名称/逗号之间没有空格?有没有办法在没有/使用正则表达式的情况下实现这一目标?还是解析错误?

标签: phpjson

解决方案


你应该把它写成

$("#stars").val(data.Actors.join(', '))

推荐阅读