简体   繁体   English

使用PHP json_decode将JSON转换为数组

[英]JSON to Array using PHP json_decode

I'm reading a json file using 我正在使用读取JSON文件

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

and I want $jsonParsed to be a associative array like this: 并且我希望$ jsonParsed是这样的关联数组:

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

What is the format of the JSON needed to do this? 所需的JSON格式是什么?

I tried this 我试过了

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

but even with JSONLint saying this piece og JSON is valid, I can't get the result I need. 但是,即使JSONLint说这段JSON是有效的,我也无法获得所需的结果。

I'm not really very used to JSON and then I will appreciate a lot any help given. 我不太习惯JSON,因此我将不胜感激。

EDITED: 编辑:

This is the function I'm using: 这是我正在使用的功能:

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

My goal is to fill this array $this->connectionParams with data extracted from the JSON file. 我的目标是用从JSON文件提取的数据填充此数组$ this-> connectionParams。

I notice that you are trying to decode the filename instead of the content. 我注意到您正在尝试解码文件名而不是内容。

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

Shouldn't it be 不是吗

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

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

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