0

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.

17
  • 2
    Works fine: eval.in/408693 Commented Jul 30, 2015 at 3:15
  • Also works: eval.in/408694 Commented Jul 30, 2015 at 3:15
  • Not here, unfortunately, @Jessica. I get a NULL, like if the JSON were malformed. Commented Jul 30, 2015 at 3:17
  • are you sure $jsonStr contains what you think it does Commented Jul 30, 2015 at 3:17
  • 2
    I notice you are trying to decode the filename @EdvaldoAlmeidaJr. not the file content. Commented Jul 30, 2015 at 3:34

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.