简体   繁体   中英

Modifying data structure that will be encoded using JSON in perl

I have a JSON whcih i am trying to modify using perl code.

{
    "Person":{

       "personalData": {
          "workList": {
              "file":{ 
                  "fileName": "/usr/temp/ABC.txt" }
                },
            }
         }
       }   
    }  

I need to convert the above JSON into something like this:

{
    "Person":{

       "personalData": {
          "workList": {
              "directoryList":{
                   "directory":[
                       "file":{ 
                           "fileName": "/usr/temp/ABC.txt" }
                          }
                     ]
                }
            }
         }
       }   
    }

Can someone give some example of doing this in perl.

$data->{Person}{personalData}{workList}{directoryList}{directory} =
    [ delete $data->{Person}{personalData}{workList}{file} ];

Or more concisely,

$tmp = $data->{Person}{personalData}{workList};
$tmp->{directoryList}{directory} = [ delete $tmp->{file} ];

For an explanation of how this works see: How to replace a Perl hash key?

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