简体   繁体   中英

JSON to Array using PHP json_decode

I'm reading a json file using

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

and I want $jsonParsed to be a associative array like this:

$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?

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.

I'm not really very used to JSON and then I will appreciate a lot any help given.

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.

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

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