如何将数组正确转换为json_encode

时间:2018-08-13 01:59:09

标签: javascript php json ajax

我发现了这一点,它非常有帮助...

Demo: jQuery Ajax Call to PHP Script with JSON Return

但是我在使用它时遇到了一些问题。...

我需要了解如何正确使用它...

我不明白为什么我的数组无法在json中转换。...

这些代码在response.php中:

from datetime import datetime  
from datetime import timedelta  

turns = 2
dayson = 11
dayoff = 17
total_day = 0

for j in range(0,(turns)):
    for k in range(0,dayson):
        d = datetime.now() + timedelta(days=total_day)
        dt = d.strftime('%m/%d/%Y') + " Do Task"
        print(dt)
        total_day += 1
    for l in range(0,dayoff):
        d = datetime.now() + timedelta(days=total_day)
        dt = d.strftime('%m/%d/%Y') + " Don't Do Task"
        print(dt)
        total_day += 1

在我的index.html中,是javascript部分:

08/12/2018 Do Task
08/13/2018 Do Task
08/14/2018 Do Task
08/15/2018 Do Task
08/16/2018 Do Task
08/17/2018 Do Task
08/18/2018 Do Task
08/19/2018 Do Task
08/20/2018 Do Task
08/21/2018 Do Task
08/22/2018 Do Task
08/23/2018 Don't Do Task
08/24/2018 Don't Do Task
08/25/2018 Don't Do Task
08/26/2018 Don't Do Task
08/27/2018 Don't Do Task

警报不会随我需要使用的东西一起回来。...

结果给出了:

$champs = ["user" => "", "combinaison" => 0, "valeurSmall" => "", "valeurBig" => "", "aucune_valeur" => false, "trop_valeur" => false, "color_red" => 0, "color_green" => 0, "color_blue" => 0];

$return["json"] = json_encode($champs, JSON_FORCE_OBJECT); 
echo json_encode($return); 

有什么想法吗?!??!

Thx伙计们

1 个答案:

答案 0 :(得分:0)

您是否在php文件顶部提供了用于响应输出的标头json,如下所示?

<?php header("Content-type: application/json; charset=utf-8"); ?>