简体   繁体   English

使用json_encode()在php文件中表示值的JSON

[英]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. 我需要帮助才能正确使用json_encode()在我的php服务器脚本中返回值的JSON表示。 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 如何从我的data.json文件中获取一个“值”

    {
    "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? 进入我的php服务器脚本中相应的PHP变量?

    $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}. 该值应该在php服务器脚本计算签名内给出有效的JSON响应,否则它将{“invalid”:true}。 Thanx for your help! Thanx为您提供帮助!

To get the data from a JSON file, you use json_decode() , not json_encode() . 要从JSON文件获取数据,请使用json_decode() ,而不是json_encode() Then you access the parts of it using normal PHP object syntax. 然后使用普通的PHP对象语法访问它的各个部分。

$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: 1.您需要解码JSON才能使其可用:

$json = json_decode($jsonString, true);

Note the second parameter set to 'true', it means that we want an associative array instead of an object. 注意第二个参数设置为'true',这意味着我们需要一个关联数组而不是一个对象。

2. You can now use your JSON as a regular associative array: 2.您现在可以将JSON用作常规关联数组:

$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. 如果您想知道偏移量是否有效,可以在其上使用isset()函数。

You need to use json_decode() like so: 您需要像这样使用json_decode()

$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 示例: http//sandbox.onlinephpfunctions.com/code/8f1de6493c35cadd0976532b36a23c2fb09bc7b2

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

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