繁体   English   中英

使用PHP json_decode将JSON转换为数组

[英]JSON to Array using PHP json_decode

我正在使用读取JSON文件

$jsonStr = file_get_contents("connection.json");
$jsonParsed = json_decode($jsonStr,true);

并且我希望$ jsonParsed是这样的关联数组:

$jsonParsed = { "SERVER" => "111.222.333.444" , "USER" => "edvaldo" , "PASSWORD" => "my_password_here", "DATABASE" => "my_database_here" };

所需的JSON格式是什么?

我试过了

{
    "SERVER": "111.222.333.444",
    "USER": "edvaldo",
    "PASSWORD": "my_password_here",
    "DATABASE": "my_database_here"
}

但是,即使JSONLint说这段JSON是有效的,我也无法获得所需的结果。

我不太习惯JSON,因此我将不胜感激。

编辑:

这是我正在使用的功能:

private function set_mysql_parameters() {
    $this->connectionParams = array();
    $json_file = $this->system_paths["JSONDATA"] . "connection.json";
    $json_mysql = file_get_contents($json_file);
    $json_parsed = json_decode($json_file,true,250);
    foreach($json_parsed as $key => $value) {
        $this->connectionParams[$key] = $value;
    }
}

我的目标是用从JSON文件提取的数据填充此数组$ this-> connectionParams。

我注意到您正在尝试解码文件名而不是内容。

$json_file = $this->system_paths["JSONDATA"] . "connection.json";
$json_parsed = json_decode($json_file,true,250);

不是吗

$json_parsed = json_decode($json_mysql, true, 250);

暂无
暂无

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

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