如何以JSON格式编写内容

时间:2018-03-30 06:43:27

标签: php

我在PHP中遇到了JSON问题。

我有一个像这样的JSON结构(有些行是const,有些行是可变的)

{
    "user": {
        "lam": {---->variable
            "\/lam\/files\/a": { ---->variable
                "id": 9, ---->variable
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498", ---->variable
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU=" ---->variable
                }
            }
        }
    }
}

我不知道如何将此内容写为JSON格式的文件。 在许多情况下,我必须写入附加到此文件,结果必须如下:

{
    "user": {
        "lam": {
            "\/lam\/files\/a": {
                "id": 9,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU="
                }
            },
            "\/lam\/files\/test": {
                "id": 12,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-152153961597103330",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "MjdzcDlrenptcG5udzI2MLSQvuGIczY\/SyHZVf9o7e8="
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在使用json_encode()之前创建一个数组:

这是一个例子。

// prepare an array with variable (should come from database, or whatever)
$vars = [
  [
    'user' => 'lam',
    'path' => '/lam/files/a',
    'id' => 9,
    'bucket' => 'lam-15215156681752265498',
    'pass' => 'NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU=',
  ],
  [
    'user' => 'lam',
    'path' => '/lam/files/test',
    'id' => 12,
    'bucket' => 'lam-152153961597103330',
    'pass' => 'MjdzcDlrenptcG5udzI2MLSQvuGIczY/SyHZVf9o7e8=',
  ]
];

// prepare data to be encoded.
$data = [];

// iterate over the variables,
foreach ($vars as $var) {

  // prepare an array for the options
  $options = [
    "user" => "owncloud",
    "bucket" => $var['bucket'], // fill with current variable
    "password" => "",
    "region" => "regionOne",
    "service_name" => "swift",
    "tenant" => "owncloud",
    "timeout" => "30",
    "url" => "http:\/\/controller:5000\/v2.0",
    "password_encrypted" =>$var['pass'], // fill with current variable
  ];
  $userdata = [
    'id' => $var['id'], // fill with current variable
    'class' => '\\OC\\Files\\Storage\\Swift',
    'options' => $options, // append options
  ];
  $name = $var['user'];
  $path = $var['path'];
  $data['user'][$name][$path] = $userdata ; // append to $data array
}

echo json_encode($data, JSON_PRETTY_PRINT);

输出:

{
    "user": {
        "lam": {
            "\/lam\/files\/a": {
                "id": 9,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\\\/\\\/controller:5000\\\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU="
                }
            },
            "\/lam\/files\/test": {
                "id": 12,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-152153961597103330",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\\\/\\\/controller:5000\\\/v2.0",
                    "password_encrypted": "MjdzcDlrenptcG5udzI2MLSQvuGIczY\/SyHZVf9o7e8="
                }
            }
        }
    }
}

答案 1 :(得分:0)

您将需要将数据作为数组(或对象)来使用,因为PHP无法直接在JSON中工作;

将文件中的当前数据放入数组:

$data = json_decode(file_get_contents($pathtojsonfile, true));

将新数据追加到数组:

$data[] = $newdata;

然后将新添加的数据保存回文件。

file_put_contents($pathtojsonfile, json_encode($data));