【问题标题】:how to access to a value in JSON array containing objects如何访问包含对象的 JSON 数组中的值
【发布时间】:2018-08-13 07:04:50
【问题描述】:

我想遍历我的 JSON 结果并获取每个对象的值来对它们求和。 这是我的 JSON 结果:

[{"Duree":"01:00:00"},{"Duree":"00:30:00"},{"Duree":"01:00:00"}]

然后我在我的 Ajax 方法中执行此操作:

var xhr1 = getXhr();
    xhr1.onreadystatechange = function(){
        if(xhr1.readyState == 4 && xhr1.status == 200){
            Selection = xhr.responseText;
            for(var i = 0; i < Selection.length; i++) {
                if (i != 0) {
                    start = tot;
                }
                else if (i === 0){
                    start = Selection[0];
                }
                if ((i + 1) >= Selection.length) {
                    end = Selection[i + 1];
                    tot = addTimes(start, end);
                }
            }
            alert(tot);
        }
    };

生成我的 json 输入的我的 php 代码:

foreach($rep as $Intervention) {
    if ($Intervention['Vacation'] == $idVacation) {
        $Query = 'SELECT Duree FROM fairegammeoperatoire WHERE IDIntervention=:id';
        $rep = $bdd->prepare($Query);
        $custom = $Intervention['IDIntervention'];
        $rep->bindParam(':id',$custom);
        $rep->execute();
        $Duree = $rep->fetch(PDO::FETCH_ASSOC);
        $tot = $Duree;
        array_push($total, $tot);
    }
}
echo json_encode($total);

编辑:问题是 JSON.parse 中使用的变量 xhr 的名称不是好的名称。必须是 xhr1 而不是 xhr。全部=D

【问题讨论】:

    标签: javascript php json ajax time


    【解决方案1】:

    您是否尝试将其解析为 json 对象: 它必须被解析,因为响应将被读取为字符串。

    var xhr1 = getXhr();
        xhr1.onreadystatechange = function(){
            if(xhr1.readyState == 4 && xhr1.status == 200){
                Selection = JSON.parse(xhr1.responseText);
                for(var i = 0; i < Selection.length; i++) {
                    if (i != 0) {
                        start = tot;
                    }
                    else if (i === 0){
                        start = Selection[0].Duree;
                    }
                    if ((i + 1) >= Selection.length) {
                        end = Selection[i + 1].Duree;
                        tot = addTimes(start, end);
                    }
                }
                alert(tot);
            }
        };
    

    你的 json 必须是这样的

    <?php 
    $dataArray = array(array("Duree"=>"01:00:00"),array("Duree"=>"02:00:00"),array("Duree"=>"03:00:00"),array("Duree"=>"04:00:00"));
    echo json_encode($dataArray);
    ?>
    

    【讨论】:

    • 我试过了,但有人对我说:如果你已经在服务器端进行了 json_encode,你就不必在客户端使用 JSON.parse。这就是为什么我有错误“JSON 中的意外结束”
    • 不可能,Ajax 会将响应读取为字符串,因此,除非您再次将字符串解析为 json,否则您将无法以 josn 的形式访问它。
    • 作为一个普通对象,所以我做 Selection.i 或 Selection[i].Duree ?或选择->Duree?
    • 选择[i].Duree
    • 未解析的变量 Duree,我再次遇到“JSON.parse() 处的 JSON 输入意外结束。对我来说,我的 php 文件的预览部分中显示的 json 语法没问题...... object 是 json 的神语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多