duanji1610 2018-01-25 04:03
浏览 1010

JSON.parse不起作用

I have a PHP function that simply calls the database for an array of results and then outputs it as a JSON object like so:

PHP:

function getSubscriptionDetails(){
    $subscription = $this->client_model->getSubscription($this->user->info->ID);
    header('Content-Type: application/json');
    echo json_encode($subscription);
}

Result:

{
    maxDenomination: "100",
    startDate: "2018-01-19 19:44:41",
    endDate: "2018-02-19 19:44:41",
    subscriptionID: "8",
    packageID: "1",
    packageName: "Silver"
}

My goal is to be able to use this in my javascript AJAX call but when I try and run JSON.parse on it, it says its not valid.

From what I gathered, valid JSON is supposed to have quoted key names as well. I glanced through json_encode but couldn't see anything that enabled this.

Am I missing something here? I am not manually creating the array so I can't quote them myself, was hoping it was just a option I could pass with the encoding.

  • 写回答

1条回答 默认 最新

  • doudou3935 2018-01-25 04:10
    关注

    your result a json object you dnt need to parse it if it is a json string you need to prase it

    to get the value of maxDenomination try the below

    var a = {
        maxDenomination: "100",
        startDate: "2018-01-19 19:44:41",
        endDate: "2018-02-19 19:44:41",
        subscriptionID: "8",
        packageID: "1",
        packageName: "Silver"
    }
    
    console.log(a.maxDenomination);
    
    评论

报告相同问题?