为什么带有花括号的JSON无效?

时间:2018-11-10 19:30:09

标签: php json datatables

我正在尝试将有效JSON传递给dataTables,但是不接受在数组中使用花括号的任何结果。

工作JSON数据[demo]:

{
  "data":[

    [
      "Rhona Davidson",
      "Integration Specialist",
      "Tokyo",
      55,
      "2010/10/14",
      "$327,900"
    ],
    [
      "Colleen Hurst",
      "Javascript Developer",
      "San Francisco",
      39,
      "2009/09/15",
      "$205,500"
    ],
    [
      "Sonya Frost",
      "Software Engineer",
      "Edinburgh",
      23,
      "2008/12/13",
      "$103,600"
    ],
    [
      "Jena Gaines",
      "Office Manager",
      "London",
      30,
      "2008/12/19",
      "$90,560"
    ],
    [
      "Quinn Flynn",
      "Support Lead",
      "Edinburgh",
      22,
      "2013/03/03",
      "$342,000"
    ],
    [
      "Charde Marshall",
      "Regional Director",
      "San Francisco",
      36,
      "2008/10/16",
      "$470,600"
    ],
    [
      "Haley Kennedy",
      "Senior Marketing Designer",
      "London",
      43,
      "2012/12/18",
      "$313,500"
    ],
    [
      "Jenette Caldwell",
      "Development Lead",
      "New York",
      30,
      "2011/09/03",
      "$345,000"
    ],
    [
      "Yuri Berry",
      "Chief Marketing Officer (CMO)",
      "New York",
      40,
      "2009/06/25",
      "$675,000"
    ],
    [
      "Caesar Vance",
      "Pre-Sales Support",
      "New York",
      21,
      "2011/12/12",
      "$106,450"
    ],
    [
      "Doris Wilder",
      "Sales Assistant",
      "Sidney",
      23,
      "2010/09/20",
      "$85,600"
    ],
    [
      "Angelica Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      47,
      "2009/10/09",
      "$1,200,000"
    ],
    [
      "Gavin Joyce",
      "Developer",
      "Edinburgh",
      42,
      "2010/12/22",
      "$92,575"
    ],
    [
      "Jennifer Chang",
      "Regional Director",
      "Singapore",
      28,
      "2010/11/14",
      "$357,650"
    ],
    [
      "Brenden Wagner",
      "Software Engineer",
      "San Francisco",
      28,
      "2011/06/07",
      "$206,850"
    ],
    [
      "Fiona Green",
      "Chief Operating Officer (COO)",
      "San Francisco",
      48,
      "2010/03/11",
      "$850,000"
    ],
    [
      "Shou Itou",
      "Regional Marketing",
      "Tokyo",
      20,
      "2011/08/14",
      "$163,000"
    ],
    [
      "Michelle House",
      "Integration Specialist",
      "Sidney",
      37,
      "2011/06/02",
      "$95,400"
    ],
    [
      "Suki Burks",
      "Developer",
      "London",
      53,
      "2009/10/22",
      "$114,500"
    ],
    [
      "Prescott Bartlett",
      "Technical Author",
      "London",
      27,
      "2011/05/07",
      "$145,000"
    ],
    [
      "Gavin Cortez",
      "Team Leader",
      "San Francisco",
      22,
      "2008/10/26",
      "$235,500"
    ],
    [
      "Martena Mccray",
      "Post-Sales support",
      "Edinburgh",
      46,
      "2011/03/09",
      "$324,050"
    ],
    [
      "Unity Butler",
      "Marketing Designer",
      "San Francisco",
      47,
      "2009/12/09",
      "$85,675"
    ],
    [
      "Howard Hatfield",
      "Office Manager",
      "San Francisco",
      51,
      "2008/12/16",
      "$164,500"
    ],
    [
      "Hope Fuentes",
      "Secretary",
      "San Francisco",
      41,
      "2010/02/12",
      "$109,850"
    ],
    [
      "Vivian Harrell",
      "Financial Controller",
      "San Francisco",
      62,
      "2009/02/14",
      "$452,500"
    ],
    [
      "Timothy Mooney",
      "Office Manager",
      "London",
      37,
      "2008/12/11",
      "$136,200"
    ],
    [
      "Jackson Bradshaw",
      "Director",
      "New York",
      65,
      "2008/09/26",
      "$645,750"
    ],
    [
      "Olivia Liang",
      "Support Engineer",
      "Singapore",
      64,
      "2011/02/03",
      "$234,500"
    ]
  ]
}

JSON数据失败(演示):

  

{“数据”:[{“ id”:“ in_1DLk6ZLrfrJRnR7ZLFRmoZAp”,“已支付金额”:15911}]}

返回

{"data":[{"id":"in_1DLk6ZLrfrJRnR7ZLFRmoZAp","amount-paid":15911}]}

该演示程序从PHP文件返回内容。

 header("HTTP/1.1 200 OK");
 foreach ($api_response['data'] as $customer) {
 $return_array[] = array(
     'id' => $customer['id'], 'amount-paid' => $customer['amount_paid']);
 }

 $mainArray['data'] = $return_array;
 echo json_encode($mainArray);

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果您没有字段的标签名称,则只能使用after格式。

$return_array[] = array(
     $customer['id'], $customer['amount_paid']);
 }

一旦包含标签,就意味着数据是对象而不是数组。

答案 1 :(得分:0)

我认为问题不在于Json的内容,而是您的echoPost.php的内容类型 数据表正在验证响应的内容类型。并且必须为application/json才能有效

尝试此代码:

 header("HTTP/1.1 200 OK");
 header("Content-Type: application/json");
 foreach ($api_response['data'] as $customer) {
 $return_array[] = array(
     'id' => $customer['id'], 'amount-paid' => $customer['amount_paid']);
 }

 $mainArray['data'] = $return_array;
 echo json_encode($mainArray);