簡體   English   中英

如何通過JavaScript打印多維JSON對象?

[英]How to print Multidimensional JSON Object via JavaScript?

在我的項目中,有一個像這樣的JSON對象:

{
"face": [
    {
        "attribute": {
            "age": {
                "range": 5,
                "value": 35
            },
            "gender": {
                "confidence": 99.9974,
                "value": "Male"
            },
            "glass": {
                "confidence": 98.3809,
                "value": "None"
            },
            "pose": {
                "pitch_angle": {
                    "value": 2e-05
                },
                "roll_angle": {
                    "value": -6.70428
                },
                "yaw_angle": {
                    "value": 0.005689
                }
            },
            "race": {
                "confidence": 61.7833,
                "value": "Black"
            },
            "smiling": {
                "value": 10.932
            }
        },
        "face_id": "b8b25a0fbb13f149a87438047fdd3e18",
        "position": {
            "center": {
                "x": 50.973236,
                "y": 55.583333
            },
            "eye_left": {
                "x": 36.641119,
                "y": 47.448167
            },
            "eye_right": {
                "x": 65.265207,
                "y": 45.143333
            },
            "height": 42.166667,
            "mouth_left": {
                "x": 41.970316,
                "y": 68.837667
            },
            "mouth_right": {
                "x": 65.547445,
                "y": 68.029667
            },
            "nose": {
                "x": 52.064964,
                "y": 58.712167
            },
            "width": 61.557178
        },
        "tag": ""
    }
],
"img_height": 629,
"img_id": "9ea3cab5193d005e89be75fb92f1bd88",
"img_width": 431,
"session_id": "8e57472a5d1542a4943237e0bfd8798f",
"url": null
}

還有一個這樣的JavaScript腳本,我想顯示年齡范圍:

<script>
    var txt1 = '<?php echo $response['body']; ?>';
    objF = JSON.parse(txt1);
    document.getElementById("fac").innerHTML = objF.face[0].attribute.age.range;
</script>

所以請幫我解決這個問題。

只需從您的php腳本標簽周圍刪除'符號,就無需解析JSON

var txt1 = <?php echo $response['body']; ?>;
document.getElementById("fac").innerHTML = objF.face[0].attribute.age.range;

從第一行中刪除

var txt1 = <?php echo $response['body']; ?>;
objF = JSON.parse(txt1);
document.getElementById("fac").innerHTML = objF.face[0].attribute.age.range;

這就對了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM