简体   繁体   中英

Use json_encode() for JSON representation of a value in php file

I need help to properly use json_encode() to return a JSON representation of a value in my php server script. As far as i learned, this is not done with echo, print or loop as explained in all the other questions i studied before asking.

How do i get one "Value" from my data.json file

    {
    "clientPrivateKey": {
      "Name":"AWS_CLIENT_SECRET_KEY",
      "Value":"someexammplestring"
      },
    "serverPublicKey": {
      "Name":"AWS_SERVER_PUBLIC_KEY",
      "Value":"someexammplestring"
      },
    "serverPrivateKey": {
      "Name":"AWS_SERVER_PRIVATE_KEY",
      "Value":"someexammplestring"
      },
    "expectedBucketName": {
      "Name":"S3_BUCKET_NAME",
      "Value":"someexammplestring"
      }
    }

into the corresponding PHP variable in my php server script?

    $clientPrivateKey =
    $serverPublicKey =
    $serverPrivateKey =
    $expectedBucketName =

I only need the "Value" string here. The value is supposed to give a valid JSON response inside the php server script calculating signatures or else it will {"invalid":true}. Thanx for your help!

To get the data from a JSON file, you use json_decode() , not json_encode() . Then you access the parts of it using normal PHP object syntax.

$json = file_get_contents("data.json");
$data = json_decode($json);
$clientPrivateKey = $data->clientPrivateKey->Value;
$serverPublicKey = $data->serverPublicKey->Value;
$serverPrivateKey = $data->serverPrivateKey->Value;
$expectedBucketName = $data->expectedBucketName->Value;

1. You need to decode your JSON to make it usable:

$json = json_decode($jsonString, true);

Note the second parameter set to 'true', it means that we want an associative array instead of an object.

2. You can now use your JSON as a regular associative array:

$clientPrivateKey = $json['clientPrivateKey']['Value'];

You can access the two others values you want by following the previous example. If you want to know if the offset is valid you can use the isset() function on it.

You need to use json_decode() like so:

$json = json_decode({
"clientPrivateKey": {
    "Name":"AWS_CLIENT_SECRET_KEY",
    "Value":"someexammplestring"
},
"serverPublicKey": {
    "Name":"AWS_SERVER_PUBLIC_KEY",
    "Value":"someexammplestring"
},
"serverPrivateKey": {
    "Name":"AWS_SERVER_PRIVATE_KEY",
    "Value":"someexammplestring"
},
"expectedBucketName": {
    "Name":"S3_BUCKET_NAME",
    "Value":"someexammplestring"
}
},true);

$clientPrivateKey = $json['clientPrivateKey']['Value'];
$serverPublicKey = $json['serverPublicKey']['Value'];
$serverPrivateKey = $json['serverPrivateKey']['Value'];
$expectedBucketName = $json['expectedBucketName']['Value'];

There you go:

<?php
$json = json_decode(file_get_contents('data.json'));
$clientPrivateKey = $json->clientPrivateKey->Value;
// ...

You can use this one liner to code to automatically extract the variables from an array:

 extract(array_combine(array_keys($array=json_decode($json,true)),array_column($array,"Value")));
// $clientPrivateKey,$serverPublicKey,$serverPrivateKey,$expectedBucketName are now set

Example: http://sandbox.onlinephpfunctions.com/code/8f1de6493c35cadd0976532b36a23c2fb09bc7b2

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