简体   繁体   中英

Issue with JSON-Object in jQuery

I'm not able to get the value from a json-object in jquery. It throws the error message("Uncaught SyntaxError: Unexpected token H") in developer tool.

How can I get the value from my JSON-Object?

Please let me know.

The PHP-Code: ( unedited )

[
    {
        "qus_type": "choice",

        "qus_text": "Which one is if statement?",

        "correct_answer": "<?php$t=date("H");if ($t<"20") {  echo "Haveagoodday!";}?>",

        "choice1": "<?php$t=date("H");if ($t<"20") {  echo "Haveagoodday!";} else {  echo                "Haveagoodnight!";}?>",

        "choice2": "<?php$t=date("H");if ($t<"20") {  echo "Haveagoodday!";}?>",

        "choice3": "<?php$t=date("H");if ($t<"10") {  echo "Haveagoodmorning!";} elseif ($t<"20") {  echo "Haveagoodday!";} else {  echo "Haveagoodnight!";}?>",

        "choice4": "",

        "choice5": ""
    }
]

The Javascript / jQuery-part:

jsonObj = jQuery.parseJSON(ques_json);

window.jQuery(jsonObj).each(function(index, item) {
    console.log(item.qus_type);
    console.log(item.qus_text);
    console.log(item.correct_answer);
    console.log(item.choice1);
    console.log(item.choice2);
    console.log(item.choice3);
    console.log(item.choice4);
    console.log(item.choice5);

})

Couple of typos -

1- <?php$t= needs a space between <?php and $t - <?php $t=

2 - You are reusing a quotation (") in "<?php$t=date("H"); . You need to use an apostrophe in place of one of the sets. Also, you are declaring a < on a string in your if/else (within quotations), int/numbers don't use quotations so maybe...

"choice3": "<?php$t=date('H');if ($t < 10) {  echo 'Haveagoodmorning!';} elseif ($t < 20) {  echo 'Haveagoodday!';} else {  echo 'Haveagoodnight!';}?>",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM