简体   繁体   English

JSON.parse不起作用

[英]JSON.parse does not work

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函数,该函数只是调用数据库以获取结果数组,然后将其输出为JSON对象,如下所示:

PHP: 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. 我的目标是能够在我的javascript AJAX调用中使用此功能,但是当我尝试对其运行JSON.parse时,它表示无效。

From what I gathered, valid JSON is supposed to have quoted key names as well. 从我收集到的信息来看,有效的JSON应该也带有引号。 I glanced through json_encode but couldn't see anything that enabled this. 我浏览了json_encode但看不到任何启用此功能的内容。

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. 我不是手动创建数组,所以我自己也不能引用它们,希望这只是我可以通过编码传递的一个选项。

your result a json object you dnt need to parse it if it is a json string you need to prase it 您的结果是一个json对象,如果是json字符串,则需要解析它

to get the value of maxDenomination try the below 要获得maxDenomination的价值,请尝试以下

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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