简体   繁体   English

需要帮助阅读和更新 json 文件 PHP

[英]Need help reading and updating json file with PHP

I have about 3000 json files in a folder that I need to edit.我需要编辑的文件夹中有大约 3000 个 json 文件。 I have done some research but everything I have found does not seem to work for my situation.我做了一些研究,但我发现的一切似乎都不适合我的情况。 I believe it's because when extracting the data to an array the the data from attributes is in a sub array.我相信这是因为将数据提取到数组时,属性中的数据位于子数组中。

I need to open each file and edit the "trait_type" name.我需要打开每个文件并编辑"trait_type"名称。 I need to remove Vehicle/ for each trait type.我需要为每种特征类型删除 Vehicle/ 。

For example I need to edit例如我需要编辑

"trait_type": "Vehicle/Exterior"

and change to this.并改成这个。

"trait_type": "Exterior"

Any help would be much appreciated.任何帮助将非常感激。

JSON file JSON 档案

{
    "name":"2020 Mercedes-Benz Sl550 ",
    "description":"Fully Loaded",
    "image":"http://www.websitename.com/images/2020_mercedes-benz_1.jpg",
    "price":"69900",
    "miles":4500,
    "date":1647530418343,
    "attributes":[
        {
            "trait_type":"Vehicle/Exterior",
            "value":"Black"
        },
        {
            "trait_type":"Vehicle/Interior",
            "value":"Black"
        },
        {
            "trait_type":"Vehicle/Engine",
            "value":"4.7L V8 32V"
        },
        {
            "trait_type":"Vehicle/Fuel",
            "value":"GAS"
        }
    ],
    "Stock Number":"43212"
}
  • Use glob to fetch all JSON filenames使用glob获取所有 JSON 个文件名
  • For each file, read the content and convert it to stdClass对于每个文件,读取内容并将其转换为stdClass
  • Cycle through attributes , do a simple str_replace on any occurrence of trait_type循环遍历attributes ,对任何出现的trait_type进行简单的str_replace
  • Convert the stdClass back to JSON stringstdClass转换回 JSON 字符串
  • Overwrite each file with the newly replaced content用新替换的内容覆盖每个文件

$files = glob('*.json');
foreach ($files as $file) {
  $json = json_decode(file_get_contents($file));
  foreach($json->attributes as $attrib) {
    if (isset($attrib->trait_type)) {
      $attrib->trait_type = str_replace('Vehicle/', '', $attrib->trait_type);
    }
  }
  file_put_contents($file, json_encode($json, JSON_PRETTY_PRINT));  
}

You should skip JSON_PRETTY_PRINT , just using it for demonstration purposes, so it is easier to see that Vehicle/ is actually removed.您应该跳过JSON_PRETTY_PRINT ,仅将其用于演示目的,因此更容易看出Vehicle/实际上已被删除。 Obvious PHP should have write-access to the directory.显然 PHP 应该对该目录具有写访问权限。

Ohh, didn't make in time:D哦,没及时:D

<?php

if(!is_dir('fixed')){
    mkdir('fixed');
}

foreach(glob('*.json') as $json_file){
    
    $data = json_decode(file_get_contents($json_file));

    foreach($data->attributes as $key => $attribute){

        $data->attributes[$key]->trait_type = preg_replace('/^vehicle\//i', '', $attribute->trait_type);

    }

    file_put_contents('fixed/'.$json_file, json_encode($data, JSON_PRETTY_PRINT));

}

You can usearray_walk_recursive , it will update the array and you can encode it again to JSON (use JSON constants to configure the json_encode behavior):您可以使用array_walk_recursive ,它会更新数组,您可以再次将其编码为 JSON (使用JSON 常量来配置json_encode行为):

<?php
// better use a named function if you do it very often
function my_modifier (&$v, $k){
    if ($k == 'trait_type') 
        $v = 'XYZ';
}

$data = '{"name":"2020 Mercedes-Benz Sl550 ","description":"Fully Loaded","image":"http:\/\/www.websitename.com\/images\/2020_mercedes-benz_1.jpg","price":"69900","miles":4500,"date":1647530418343,"attributes":[{"trait_type":"Vehicle\/Exterior","value":"Black"},{"trait_type":"Vehicle\/Interior","value":"Black"},{"trait_type":"Vehicle\/Engine","value":"4.7L V8 32V"},{"trait_type":"Vehicle\/Fuel","value":"GAS"}],"Stock Number":"43212"}';

$arr = json_decode($data, true);
array_walk_recursive($arr, 'my_modifier');
$data = json_encode($arr, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
// done, $data is upadted.
echo "<pre>$data";

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

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