Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1685 connectés 

  FORUM HardWare.fr
  Programmation
  Perl

  [Perl] Module JSON / Lire un fichier

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Perl] Module JSON / Lire un fichier

n°2313977
Sethenssen
Posté le 13-04-2018 à 17:29:25  profilanswer
 

Bonjour,
 
Je test le module JSON pour extraire des données, cela fonctionne quand mon fichier ne contient qu'une entrée.
Du coup j'essaye avec 2 entrées mais cela ne fonctionne pas et je ne sais pas pourquoi
Sachant que je vais avoir ~100 000 entrées dans mon fichier final
Voici le code:
 

Code :
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use JSON qw( decode_json );
  5.  
  6. my $json_f = '/data/scripts/cdsm/fichier.json';
  7.  
  8. my $json_text = do {
  9.        open (TOP, "<", $json_f);
  10.        local $/;
  11.        <TOP>
  12. };
  13.  
  14.  
  15. my $decoded = decode_json($json_text);
  16.  
  17. print "ContentName = " . $decoded->{"contents[0].contentName"} . "\n";
  18. print "Provider = " . $decoded->{"provider"} . "\n";
  19. print "Generic = " . $decoded->{"mGeneric2"} . "\n";
  20. for (my $i = 0 ; $i <= 5; $i++) {
  21.        print "Content = " . $decoded->{"contents[$i].contentName"} . "\n";
  22. }
  23.  
  24. __END__


 
Voici un fichier avec une entrée:

Code :
  1. {"opcode":"1002","structtype":"409","mPackageName":"MLK/P0000000000116758","err_code":"0","err_text":"","provider":"MLK","product":"MOD","titleBrief":"","title":"","assetID":"","assetName":"Is_package","CreationDate":"1256770800","major":"1","minor":"0","nExpires":"1595372400","nExpireWindow":"15","nRealExpireTS":"1596668400","mStartTriggerTime":"0","NumberOfContents":"2","contents[0].contentName":"116758.mpg","contents[0].mCreateTime":"1276127660","contents[0].mLastModifiedTime":"1276127752","content[0].mGeneric1":"","contents[1].contentName":"116759.mpg","contents[1].mCreateTime":"1276127660","contents[1].mLastModifiedTime":"1276127665","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"0000000000116758:0000000000116758:0000000000116758:0000000000116759"}


 
En sortie j'ai bien:

Code :
  1. ContentName = 116758.mpg
  2. Provider = MLK
  3. Generic = P0000000000116758:T0000000000116758:M0000000000116758:A0000000000116759
  4. Content = 116758.mpg
  5. Content = 116759.mpg
  6. Content =
  7. Content =
  8. Content =
  9. Content =


 
 
Voici un fichier avec 2 entrées:

Code :
  1. {"opcode":"1002","structtype":"409","mPackageName":"MLK/P0000000000116758","err_code":"0","err_text":"","provider":"MLK","product":"MOD","titleBrief":"","title":"","assetID":"","assetName":"Prédictions_package","CreationDate":"1256770800","major":"1","minor":"0","nExpires":"1595372400","nExpireWindow":"15","nRealExpireTS":"1596668400","mStartTriggerTime":"0","NumberOfContents":"2","contents[0].contentName":"116758.mpg","contents[0].mCreateTime":"1276127660","contents[0].mLastModifiedTime":"1276127752","content[0].mGeneric1":"","contents[1].contentName":"116759.mpg","contents[1].mCreateTime":"1276127660","contents[1].mLastModifiedTime":"1276127665","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"P0000000000116758:T0000000000116758:M0000000000116758:A0000000000116759"},
  2. {"opcode":"1002","structtype":"409","mPackageName":"MLK/P0000000000116762","err_code":"0","err_text":"","provider":"MLK","product":"MOD","titleBrief":"","title":"","assetID":"","assetName":"Prédictions_HD_package","CreationDate":"1256770800","major":"1","minor":"0","nExpires":"1595372400","nExpireWindow":"15","nRealExpireTS":"1596668400","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"116762.mpg","contents[0].mCreateTime":"1276126943","contents[0].mLastModifiedTime":"1276127253","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"P0000000000116762:T0000000000116762:M0000000000116762"}


 
L'erreur que j'ai est:

Code :
  1. garbage after JSON object, at character offset 1276 (before ",\n{"opcode":"1002",..." ) at toto.pl line 15, <TOP> line 1


 
Donc il y a quelque chose qui n'aime pas avec le local $/ j'imagine, mais je ne sais pas comment lui dire que ce sont 2 lignes différentes
 
Merci d'avance pour votre aide.

mood
Publicité
Posté le 13-04-2018 à 17:29:25  profilanswer
 

n°2314005
gilou
Modérateur
Modzilla
Posté le 14-04-2018 à 12:51:02  profilanswer
 

C'est normal, puisque tu ne donnes pas à decode_json du json valide.
Si tu mets ton contenu (des lignes, donc de tableau) dans un tableau, il y aura pas de pb.
decode_json('['.$json_text.']');
 

Code :
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use autodie;
  5. use JSON qw( decode_json );
  6.  
  7. sub json_decode_file($) {
  8.    open (my $file, "<", shift);
  9.    my $json_text = do { local $/; <$file> };
  10.    my $decoded = decode_json('['.$json_text.']');
  11.    foreach (@$decoded) {
  12.         print "ContentName = " . $_->{"contents[0].contentName"} . "\n";
  13.         print "Provider = " . $_->{"provider"} . "\n";
  14.         print "Generic = " . $_->{"mGeneric2"} . "\n";
  15.         for (my $i = 0 ; $i <= 5; $i++) {
  16.             print "Content = " . $_->{"contents[$i].contentName"} . "\n";
  17.         }
  18.         print "\n";
  19.    }
  20. }
  21.  
  22. json_decode_file('fichier.json');


 
en sortie

ContentName = 116758.mpg
Provider = MLK
Generic = P0000000000116758:T0000000000116758:M0000000000116758:A0000000000116759
Content = 116758.mpg
Content = 116759.mpg
Content =
Content =
Content =
Content =
 
ContentName = 116762.mpg
Provider = MLK
Generic = P0000000000116762:T0000000000116762:M0000000000116762
Content = 116762.mpg
Content =
Content =
Content =
Content =
Content =


 
Note: comme decode_json renvoie un scalaire, $decoded, et que l'on décode un tableau, $decoded ne peut être qu'une référence au tableau, et on récupère le tableau en faisant @{$decoded} ou en simplifié, @$decoded.
Je ne ferme pas le fichier ouvert, car cela sera fait automatiquement à la sortie de json_decode_file (et les erreurs à son ouverture seront gérées automatiquement avec autodie).
 
A+,


Message édité par gilou le 14-04-2018 à 12:58:07

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2314013
Sethenssen
Posté le 14-04-2018 à 18:10:45  profilanswer
 

Merci Gilou !
C'est parfait
 
Dernière petite chose,
Mon fichier commence par un {PACKAGE.db:{...}}
 
As-tu une petite astuce pour zapper cela ?
 
Ex:

Code :
  1. {PACKAGE.db:{"opcode":"1002","structtype":"409","mPackageName":"MLK/P0000000000116758","err_code":"0","err_text":"","provider":"MLK","product":"MOD","titleBrief":"","title":"","assetID":"","assetName":"_package","CreationDate":"1256770800","major":"1","minor":"0","nExpires":"1595372400","nExpireWindow":"15","nRealExpireTS":"1596668400","mStartTriggerTime":"0","NumberOfContents":"2","contents[0].contentName":"116758.mpg","contents[0].mCreateTime":"1276127660","contents[0].mLastModifiedTime":"1276127752","content[0].mGeneric1":"","contents[1].contentName":"116759.mpg","contents[1].mCreateTime":"1276127660","contents[1].mLastModifiedTime":"1276127665","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"P0000000000116758:T0000000000116758:M0000000000116758:A0000000000116759"},
  2. {"opcode":"1002","structtype":"409","mPackageName":"MLK/P0000000000116762","err_code":"0","err_text":"","provider":"MLK","product":"MOD","titleBrief":"","title":"","assetID":"","assetName":"HD_package","CreationDate":"1256770800","major":"1","minor":"0","nExpires":"1595372400","nExpireWindow":"15","nRealExpireTS":"1596668400","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"116762.mpg","contents[0].mCreateTime":"1276126943","contents[0].mLastModifiedTime":"1276127253","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"P0000000000116762:T0000000000116762:M0000000000116762"}}


 
 
Merci d'avance

n°2314024
gilou
Modérateur
Modzilla
Posté le 15-04-2018 à 04:04:10  profilanswer
 

Ça serait pas plutôt quelque chose de valide en json, comme
{"PACKAGE.db":[
{"opcode":"1002","structtype":"409"...},
{"opcode":"1002","structtype":"409"...}
]}
 
Bref, si on pouvait savoir si au final ton fichier est un fichier json valide ou pas, ça aiderait.
 
A+,


Message édité par gilou le 15-04-2018 à 10:19:56

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2314031
Sethenssen
Posté le 15-04-2018 à 18:20:43  profilanswer
 

Bonjour,
 
Désolé pour ce manque de précision
Voici le fichier en question
 
Je dirai que c'est bien un format JSON avec un object qui englobe le tout
Mais je ne sais pas comment l'utiliser
 

Code :
  1. {PACKAGE_INFO.db:
  2. {"opcode":"1004","structtype":"409","mPackageName":"20120420_05","err_code":"0","err_text":"","provider":"file","product":"20120420_04trailer","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20120420_05","CreationDate":"1483916400","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20120420_05.ts","contents[0].mCreateTime":"1483996926","contents[0].mLastModifiedTime":"1483996926","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20120420_05:20120420_05:20120420_05"},
  3. {"opcode":"1004","structtype":"409","mPackageName":"20141218_14","err_code":"0","err_text":"","provider":"file","product":"metropolitan_droles_de_dindes_vf_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20141218_14","CreationDate":"1483657200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20141218_14.ts","contents[0].mCreateTime":"1483716378","contents[0].mLastModifiedTime":"1483716378","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20141218_14:20141218_14:20141218_14"},
  4. {"opcode":"1004","structtype":"409","mPackageName":"20161130_03","err_code":"0","err_text":"","provider":"file","product":"96280","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20161130_03","CreationDate":"1485730800","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20161130_03.ts","contents[0].mCreateTime":"1485773963","contents[0].mLastModifiedTime":"1485773963","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20161130_03:20161130_03:20161130_03"},
  5. {"opcode":"1004","structtype":"409","mPackageName":"20161130_04","err_code":"0","err_text":"","provider":"file","product":"96280","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20161130_04","CreationDate":"1485730800","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20161130_04.ts","contents[0].mCreateTime":"1485773966","contents[0].mLastModifiedTime":"1485773966","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20161130_04:20161130_04:20161130_04"},
  6. {"opcode":"1004","structtype":"409","mPackageName":"20161228_ALLO_01","err_code":"0","err_text":"","provider":"file","product":"Emission_n52_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20161228_ALLO_01","CreationDate":"1482793200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20161228_ALLO_01.ts","contents[0].mCreateTime":"1482835472","contents[0].mLastModifiedTime":"1482835472","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20161228_ALLO_01:20161228_ALLO_01:20161228_ALLO_01"},
  7. {"opcode":"1004","structtype":"409","mPackageName":"20170104_ALLO_01","err_code":"0","err_text":"","provider":"file","product":"Emission_n53_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20170104_ALLO_01","CreationDate":"1482793200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20170104_ALLO_01.ts","contents[0].mCreateTime":"1482835529","contents[0].mLastModifiedTime":"1482835529","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20170104_ALLO_01:20170104_ALLO_01:20170104_ALLO_01"},
  8. {"opcode":"1004","structtype":"409","mPackageName":"20170111_ALLO_01","err_code":"0","err_text":"","provider":"file","product":"Emission_n54_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20170111_ALLO_01","CreationDate":"1484002800","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20170111_ALLO_01.ts","contents[0].mCreateTime":"1484064499","contents[0].mLastModifiedTime":"1484064499","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20170111_ALLO_01:20170111_ALLO_01:20170111_ALLO_01"},
  9. {"opcode":"1004","structtype":"409","mPackageName":"20170118_ALLO_01","err_code":"0","err_text":"","provider":"file","product":"Emission_n55_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"20170118_ALLO_01","CreationDate":"1484002800","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"20170118_ALLO_01.ts","contents[0].mCreateTime":"1484064583","contents[0].mLastModifiedTime":"1484064583","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"20170118_ALLO_01:20170118_ALLO_01:20170118_ALLO_01"},
  10. {"opcode":"1004","structtype":"409","mPackageName":"201706_02_sd","err_code":"0","err_text":"","provider":"file","product":"96796","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_02_sd","CreationDate":"1494889200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_02_sd.ts","contents[0].mCreateTime":"1494923531","contents[0].mLastModifiedTime":"1494923531","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_02_sd:201706_02_sd:201706_02_sd"},
  11. {"opcode":"1004","structtype":"409","mPackageName":"201706_09_hd","err_code":"0","err_text":"","provider":"file","product":"96797","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_09_hd","CreationDate":"1494889200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_09_hd.ts","contents[0].mCreateTime":"1494923036","contents[0].mLastModifiedTime":"1494923036","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_09_hd:201706_09_hd:201706_09_hd"},
  12. {"opcode":"1004","structtype":"409","mPackageName":"201706_09_sd","err_code":"0","err_text":"","provider":"file","product":"96797","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_09_sd","CreationDate":"1494889200","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_09_sd.ts","contents[0].mCreateTime":"1494923039","contents[0].mLastModifiedTime":"1494923039","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_09_sd:201706_09_sd:201706_09_sd"},
  13. {"opcode":"1004","structtype":"409","mPackageName":"201706_10_sd","err_code":"0","err_text":"","provider":"file","product":"96773","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_10_sd","CreationDate":"1494543600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_10_sd.ts","contents[0].mCreateTime":"1494580002","contents[0].mLastModifiedTime":"1494580002","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_10_sd:201706_10_sd:201706_10_sd"},
  14. {"opcode":"1004","structtype":"409","mPackageName":"201706_14_sd","err_code":"0","err_text":"","provider":"file","product":"96768","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_14_sd","CreationDate":"1494543600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_14_sd.ts","contents[0].mCreateTime":"1494586326","contents[0].mLastModifiedTime":"1494586326","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_14_sd:201706_14_sd:201706_14_sd"},
  15. {"opcode":"1004","structtype":"409","mPackageName":"201706_16_sd","err_code":"0","err_text":"","provider":"file","product":"96765","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"201706_16_sd","CreationDate":"1494543600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"1","nRealExpireTS":"2145913200","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"201706_16_sd.ts","contents[0].mCreateTime":"1494588006","contents[0].mLastModifiedTime":"1494588006","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"201706_16_sd:201706_16_sd:201706_16_sd"},
  16. {"opcode":"1004","structtype":"409","mPackageName":"lcqf20100905","err_code":"0","err_text":"","provider":"file","product":"LeChatQuiFume_DarktownStrutters_SD_VO_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"lcqf20100905","CreationDate":"1484607600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"lcqf20100905.ts","contents[0].mCreateTime":"1484686747","contents[0].mLastModifiedTime":"1484686747","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"lcqf20100905:lcqf20100905:lcqf20100905"},
  17. {"opcode":"1004","structtype":"409","mPackageName":"mdc201506_003","err_code":"0","err_text":"","provider":"file","product":"96350","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"mdc201506_003","CreationDate":"1487199600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"mdc201506_003.ts","contents[0].mCreateTime":"1487251197","contents[0].mLastModifiedTime":"1487251197","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"mdc201506_003:mdc201506_003:mdc201506_003"},
  18. {"opcode":"1004","structtype":"409","mPackageName":"we018","err_code":"0","err_text":"","provider":"file","product":"SD_VF_trailer1","titleBrief":"(null)","title":"(null)","assetID":"","assetName":"we018","CreationDate":"1482447600","major":"1","minor":"1","nExpires":"2145826800","nExpireWindow":"15","nRealExpireTS":"2147122800","mStartTriggerTime":"0","NumberOfContents":"1","contents[0].contentName":"we018.ts","contents[0].mCreateTime":"1482489748","contents[0].mLastModifiedTime":"1482489748","content[0].mGeneric1":"","contents[1].contentName":"","contents[1].mCreateTime":"0","contents[1].mLastModifiedTime":"0","content[1].mGeneric1":"","contents[2].contentName":"","contents[2].mCreateTime":"0","contents[2].mLastModifiedTime":"0","content[2].mGeneric1":"","contents[3].contentName":"","contents[3].mCreateTime":"0","contents[3].mLastModifiedTime":"0","content[3].mGeneric1":"","contents[4].contentName":"","contents[4].mCreateTime":"0","contents[4].mLastModifiedTime":"0","content[4].mGeneric1":"","contents[5].contentName":"","contents[5].mCreateTime":"0","contents[5].mLastModifiedTime":"0","content[5].mGeneric1":"","mGeneric2":"we018:we018:we018"}
  19. }

n°2314037
gilou
Modérateur
Modzilla
Posté le 15-04-2018 à 21:33:59  profilanswer
 

Non, ce n'est pas un format json.
Comme je l'ai dit, au minimum ça devrait être sous la forme
{"PACKAGE.db":[
{"opcode":"1002","structtype":"409"...},
{"opcode":"1002","structtype":"409"...}
]}  
 
C'est manifestement la sortie d'un truc qui voudrait écrire au format json mais qui est mal codé.
Par exemple
"title":"(null)" en json, ça devrait être "title":null
"contents[0].contentName":"20120420_05.ts" est un bout de tableau json, tableau a 6 éléments, bien que l'on ait "contents[0].contentName":"20120420_05.ts",
 
Bref, la première ligne, qui une fois expansée est ceci:

{
    "opcode":"1004",
    "structtype":"409",
    "mPackageName":"20120420_05",
    "err_code":"0",
    "err_text":"",
    "provider":"file",
    "product":"20120420_04trailer",
    "titleBrief":"(null)",
    "title":"(null)",
    "assetID":"",
    "assetName":"20120420_05",
    "CreationDate":"1483916400",
    "major":"1",
    "minor":"1",
    "nExpires":"2145826800",
    "nExpireWindow":"15",
    "nRealExpireTS":"2147122800",
    "mStartTriggerTime":"0",
    "NumberOfContents":"1",
    "contents[0].contentName":"20120420_05.ts",
    "contents[0].mCreateTime":"1483996926",
    "contents[0].mLastModifiedTime":"1483996926",
    "content[0].mGeneric1":"",
    "contents[1].contentName":"",
    "contents[1].mCreateTime":"0",
    "contents[1].mLastModifiedTime":"0",
    "content[1].mGeneric1":"",
    "contents[2].contentName":"",
    "contents[2].mCreateTime":"0",
    "contents[2].mLastModifiedTime":"0",
    "content[2].mGeneric1":"",
    "contents[3].contentName":"",
    "contents[3].mCreateTime":"0",
    "contents[3].mLastModifiedTime":"0",
    "content[3].mGeneric1":"",
    "contents[4].contentName":"",
    "contents[4].mCreateTime":"0",
    "contents[4].mLastModifiedTime":"0",
    "content[4].mGeneric1":"",
    "contents[5].contentName":"",
    "contents[5].mCreateTime":"0",
    "contents[5].mLastModifiedTime":"0",
    "content[5].mGeneric1":"",
    "mGeneric2":"20120420_05:20120420_05:20120420_05"
}


retranscrit un truc qui doit plutôt ressembler à

{
    "opcode":1004,
    "structtype":409,
    "mPackageName":"20120420_05",
    "err_code":0,
    "err_text":"",
    "provider":"file",
    "product":"20120420_04trailer",
    "titleBrief":null,
    "title":null,
    "assetID":"",
    "assetName":"20120420_05",
    "CreationDate":1483916400,
    "major":"1",
    "minor":"1",
    "nExpires":2145826800,
    "nExpireWindow":15,
    "nRealExpireTS":2147122800,
    "mStartTriggerTime":0,
    "NumberOfContents":1,
    "contents":[
 {
     "contentName":"20120420_05.ts",
     "mCreateTime":"1483996926",
     "mLastModifiedTime":"1483996926",
     "mGeneric1":""
 },
 {
     "contentName":"",
     "mCreateTime":0,
     "mLastModifiedTime":0,
     "mGeneric1":""
 },
 {
     "contentName":"",
     "mCreateTime":0,
     "mLastModifiedTime":0,
     "mGeneric1":""
 },
 {
     "contentName":"",
     "mCreateTime":0,
     "mLastModifiedTime":0,
     "mGeneric1":""},
 {
     "contentName":"",
     "mCreateTime":0,
     "mLastModifiedTime":0,
     "mGeneric1":""
 },
 {
     "contentName":"",
     "mCreateTime":0,
     "mLastModifiedTime":0,
     "mGeneric1":""
 }
    ],
    "mGeneric2":"20120420_05:20120420_05:20120420_05"
}

(le forum a bouffé mon formattage)
On a l'impression que l'on a évité d'imbriquer les structures dans les lignes.
 
A+,


Message édité par gilou le 15-04-2018 à 21:36:30

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2314039
gilou
Modérateur
Modzilla
Posté le 15-04-2018 à 22:19:20  profilanswer
 

Après c'est du cas par cas en fonction du format des données, par exemple:

Code :
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use autodie;
  5. use JSON qw( decode_json );
  6.  
  7. sub print_decoded_line($) {
  8.    my $decoded = shift;
  9.    print "ContentName = " . $decoded->{"contents[0].contentName"} . "\n";
  10.    print "Provider = " . $decoded->{"provider"} . "\n";
  11.    print "Generic = " . $decoded->{"mGeneric2"} . "\n";
  12.    for (my $i = 0 ; $i <= 5; $i++) {
  13.         print "Content = " . $decoded->{"contents[$i].contentName"} . "\n";
  14.    }
  15.    print "\n";
  16. }
  17.  
  18. sub json_decode_file($) {
  19.    open (my $file, "<", shift);
  20.    while (<$file> ) {
  21.         next if /^\{PACKAGE_INFO.db:/; # on saute la premiere ligne
  22.         last if /^\}/;                 # on saute la derniere ligne et on sort
  23.         s/,$//;                        # on vire la virgule en fin de ligne
  24.        my $decoded = decode_json($_); # on decode chaque ligne indépendemment
  25.         print_decoded_line($decoded);  # et on en envoie une version formatée en sortie
  26.    }
  27. }
  28.  
  29. json_decode_file('data.json');


 
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2314042
Sethenssen
Posté le 16-04-2018 à 01:29:05  profilanswer
 

Merci Gilou, très fort, je comprends mieux !


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Perl

  [Perl] Module JSON / Lire un fichier

 

Sujets relatifs
sauvegarder fichier XMLImportation de fonctions depuis un fichier
Installation module python sans internetVariable tableau dans module de classe
Creer un fichier Sass avec SublimTextModification de code sur fichier .xspf de VLC 3.0
Moyen le plus simple pour lire des fichiers CSV --> TableauBatch pour copier dernier fichier vers autre repertoire
injection donnée xls à partir d'une colone précise ?Lecture de pair<double,double> d'un fichier
Plus de sujets relatifs à : [Perl] Module JSON / Lire un fichier


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR