Page 1 sur 1

Structure de fichier json

Publié : jeu. 09/juin/2022 16:35
par michelmarchand
Bonjour,
est-ce possible de réaliser le type de structure ci-dessous en Json
une liste de tableau (Array) de données je sait le faire pour 1 Tableau
ex :Données : [ Donnée1, Donnée2, Donnée3, ......... ],
[ Donnée1, Donnée2, Donnée3, ......... ],
Comment ajouter ce deuxième tableau
ci joint l'exemple
Merci

Code : Tout sélectionner

{  "Nom de Table": "Classeur",

       "Champs"      : [
                                "Id_Row",
                                "Type de Pièce",
                                "Numéro de Pièce",
                                "Date de Création",
                                "Compte Tiers",
                                "Crédit",
                                "Débit"
                                 ]

        "Lignes"         : [
                                     "1",
                                     "Bon de Livraison",
                                     "894.350",
                                     "08/06/2022",
                                     "401049",
                                     "236.22",
                                     "0.00"
                                  ],
                                  [
                                     "2",
                                     "Facture",
                                     "22015623",
                                     "01/06/2022",
                                     "401001",
                                     "736.22",
                                     "0.00"
                                   ],
}

#JSON_Create = 0
#JSON_Parse = 1

 Procedure.s type_string( value ) ;return type as string for JSON values of null, array, or object or it's contents if a number, boolean or string
   Select JSONType(value)
     Case #PB_JSON_Null:    ProcedureReturn "null"
     Case #PB_JSON_String:  ProcedureReturn GetJSONString(value)
     Case #PB_JSON_Number:  ProcedureReturn StrD(GetJSONDouble(value))  ;integer, quad, float also  
     Case #PB_JSON_Boolean: ProcedureReturn Str(GetJSONBoolean(value))
     Case #PB_JSON_Array:   ProcedureReturn "array"
     Case #PB_JSON_Object:  ProcedureReturn "object"
   EndSelect
 EndProcedure
;
If CreateJSON( #JSON_Create )
   Table = SetJSONObject( JSONValue( #JSON_Create ) )
   NomTable = AddJSONMember( Table, "Nom de Table" )
   SetJSONString( NomTable, "Classeur" )
   Tablei = SetJSONArray( AddJSONMember( Table, "Champs") )
   
    SetJSONString( AddJSONElement( Tablei ), "Id_Row" )
    SetJSONString(AddJSONElement(Tablei), "Type de Pièce" )
    SetJSONString(AddJSONElement(Tablei), "Numéro de Pièce" )
    SetJSONString(AddJSONElement(Tablei), "Date de Création" )
    SetJSONString(AddJSONElement(Tablei), "Compte Tiers" )
    SetJSONString(AddJSONElement(Tablei), "Crédit" )
    SetJSONString(AddJSONElement(Tablei), "Débit" )
    
    Tabler = SetJSONArray(AddJSONMember( Table, "Ligne"))
    SetJSONString(AddJSONElement(Tabler), "1" )
    SetJSONString(AddJSONElement(Tabler), "Bon de Livraison" )
    SetJSONString(AddJSONElement(Tabler), "894.350" )
    SetJSONString(AddJSONElement(Tabler), "08/06/2022" )
    SetJSONString(AddJSONElement(Tabler), "401049" )
    SetJSONString(AddJSONElement(Tabler), "236.22" )
    SetJSONString(AddJSONElement(Tabler), "0.00" )
     
    Tabler = SetJSONArray(AddJSONMember( Table, "Ligne2"))
    SetJSONString(AddJSONElement(Tabler), "2" )
    SetJSONString(AddJSONElement(Tabler), "Facture" )
    SetJSONString(AddJSONElement(Tabler), "22015623" )
    SetJSONString(AddJSONElement(Tabler), "01/06/2022" )
    SetJSONString(AddJSONElement(Tabler), "401001" )
    SetJSONString(AddJSONElement(Tabler), "736.22" )
    SetJSONString(AddJSONElement(Tabler), "0.00" )
EndIf   

  Debug ComposeJSON( #JSON_Create, #PB_JSON_PrettyPrint)
  
If ExamineJSONMembers( JSONValue( #JSON_Create ) )
  While NextJSONMember( JSONValue( #JSON_Create ) )
    If JSONMemberKey( JSONValue( #JSON_Create )  ) = "Champs"
      Tableau = GetJSONMember( JSONValue( #JSON_Create ), "Champs")
         For i = 0 To JSONArraySize(Tableau ) - 1
             Debug GetJSONString( GetJSONElement( Tableau, i ) )
         Next i
 
    EndIf
  Wend
EndIf

Re: Structure de fichier json

Publié : jeu. 09/juin/2022 20:05
par GallyHC
Bonjour,

Au vu de la structure du JSON, je ne penses pas que celui-ci soit valide.

Ce serait plus juste de faire :

Code : Tout sélectionner

{
	"Nom de Table": "Classeur",
	"Lignes": [
		{
			"Id_Row": 1,
			"Type de Pièce": "Bon de Livraison",
			"Numéro de Pièce": "894.350",
			"Date de Création": "08/06/2022",
			"Compte Tiers": 401049,
			"Crédit": "236.22",
			"Débit": "0.00"
		},
		{
			"Id_Row": 2,
			"Type de Pièce": "Facture",
			"Numéro de Pièce": "22015623",
			"Date de Création": "01/06/2022",
			"Compte Tiers": 401001,
			"Crédit": "736.22",
			"Débit": "0.00"
		}
	]
}
Cordialement,
GallyHC

Re: Structure de fichier json

Publié : sam. 11/juin/2022 7:10
par michelmarchand
Bonjour GallyHC
merci d'avoir répondu
mais après avoir passé plusieurs heures à la réflexion j'ai trouvé ci-dessous

Code : Tout sélectionner

 
 ComposeJSON( #JSON_Create, #PB_JSON_PrettyPrint) 
{
  "Nom de Table": "Classeur",
  "Champs"      : [
      "Id_Row",
      "Type de Piece",
      "Numero de Piece",
      "Date de Creation",
      "Compte Tiers",
      "Credit",
      "Debit"
    ],
  "Ligne"       : [
      [
        "2",
        "Facture",
        "22015623",
        "01/06/2022",
        "401001",
        "736.22",
        "0.00"
      ],
      [
        "1",
        "Bon de Livraison",
        "894.350",
        "08/06/2022",
        "401049",
        "236.22",
        "0.00"
      ]
    ]
}


{  Affiche le Noms des Membres du Json  }
Nom de Table
Champs
Ligne


Affiche les Eléments du Membre du Json avec le nom du membre est égal à 'Champs'
[ Champs ]
----> Id_Row
----> Type de Piece
----> Numero de Piece
----> Date de Creation
----> Compte Tiers
----> Credit
----> Debit


Affiche les Eléments Tableau du Tableau est égal à 'Ligne'
[ Ligne ]
[ 2 | Facture | 22015623 | 01/06/2022 | 401001 | 736.22 | 0.00 |  ]

[ 1 | Bon de Livraison | 894.350 | 08/06/2022 | 401049 | 236.22 | 0.00 |  ]

 ; La Procédure
 
Enumeration
  #JSON_Create
  #JSON_Parse
  #JSONFile
EndEnumeration

Global  FichierJson.s = "TestSructure 1.json"

Procedure MySaveJSON()
  If ReadFile( #JSON_Create, FichierJson )
    CloseFile( #JSON_Create )
  Else
    CreateJSON( #JSON_Create )
  EndIf
  SaveJSON( #JSON_Create, FichierJson )
EndProcedure

Procedure MyDbLoad()
  If ReadFile(#JSON_Create, FichierJson )
    CloseFile(#JSON_Create)
    LoadJSON(#JSON_Create, FichierJson, #PB_JSON_NoCase)
    
    Debug " ComposeJSON( #JSON_Create, #PB_JSON_PrettyPrint) "
    Debug ComposeJSON( #JSON_Create, #PB_JSON_PrettyPrint)
  EndIf
EndProcedure


Procedure.s Json_type( value ) ;return type as string for JSON values of null, array, or object or it's contents if a number, boolean or string
  Select JSONType(value)
    Case #PB_JSON_Null:    ProcedureReturn "null"
    Case #PB_JSON_String:  ProcedureReturn GetJSONString(value)
    Case #PB_JSON_Number:  ProcedureReturn StrD(GetJSONDouble(value))  ;integer, quad, float also  
    Case #PB_JSON_Boolean: ProcedureReturn Str(GetJSONBoolean(value))
    Case #PB_JSON_Array:   ProcedureReturn "array"
    Case #PB_JSON_Object:  ProcedureReturn "object"
  EndSelect
EndProcedure
;

Procedure CreJSON()
  If CreateJSON( #JSON_Create )
    Table = SetJSONObject( JSONValue( #JSON_Create ) )
    NomTable = AddJSONMember( Table, "Nom de Table" )
    SetJSONString( NomTable, "Classeur" )
    Tablei = SetJSONArray( AddJSONMember( Table, "Champs") )
    
    SetJSONString( AddJSONElement( Tablei ), "Id_Row" )
    SetJSONString(AddJSONElement(Tablei), "Type de Pièce" )
    SetJSONString(AddJSONElement(Tablei), "Numéro de Pièce" )
    SetJSONString(AddJSONElement(Tablei), "Date de Création" )
    SetJSONString(AddJSONElement(Tablei), "Compte Tiers" )
    SetJSONString(AddJSONElement(Tablei), "Crédit" )
    SetJSONString(AddJSONElement(Tablei), "Débit" )
    
    Tabler = SetJSONArray(AddJSONMember( Table, "Ligne"))
    SetJSONString(AddJSONElement(Tabler), "1" )
    SetJSONString(AddJSONElement(Tabler), "Bon de Livraison" )
    SetJSONString(AddJSONElement(Tabler), "894.350" )
    SetJSONString(AddJSONElement(Tabler), "08/06/2022" )
    SetJSONString(AddJSONElement(Tabler), "401049" )
    SetJSONString(AddJSONElement(Tabler), "236.22" )
    SetJSONString(AddJSONElement(Tabler), "0.00" )
    
    Tablex = SetJSONArray(AddJSONMember( Table, "Ligne2"))
    SetJSONString(AddJSONElement(Tablex), "2" )
    SetJSONString(AddJSONElement(Tablex), "Facture" )
    SetJSONString(AddJSONElement(Tablex), "22015623" )
    SetJSONString(AddJSONElement(Tablex), "01/06/2022" )
    SetJSONString(AddJSONElement(Tablex), "401001" )
    SetJSONString(AddJSONElement(Tablex), "736.22" )
    SetJSONString(AddJSONElement(Tablex), "0.00" )
  EndIf
EndProcedure

;MySaveJSON()
MyDbLoad()
; ListingChamps( "Ligne" )

;"------------------------------------------"
Debug "{  Affiche le Noms des Membre du Json  }" 

IDJson.l = JSONValue( #JSON_Create )
If ExamineJSONMembers( IDJson )
  If Json_type( IDJson ) = "object"
     While NextJSONMember( IDJson )
      Debug JSONMemberKey( IDJson )
    Wend
  EndIf
  Debug ""
  Debug ""
  ;"------------------------------------------"
  Debug "Affiche les Eléments du Membre du Json avec le nom du membre est égal à 'Champs'"
  
  objet = GetJSONMember( JSONValue( #JSON_Create ), "Champs" )
  Debug "[ "+JSONMemberKey( IDJson )+" ]"
  If Json_type( objet ) = "array"
    For i = 0 To JSONArraySize( objet ) - 1
      Debug "----> "+GetJSONString( GetJSONElement( objet, i ) )
    Next i
  EndIf
  Debug ""
  Debug ""
  
  ;"------------------------------------------"
  Debug "Affiche les Eléments Tableau du Tableau est égal à 'Ligne'"
  
  objet = GetJSONMember( JSONValue( #JSON_Create ), "Ligne" )
  Debug "[ "+JSONMemberKey( IDJson )+" ]"
  If Json_type( objet ) = "array"
    For i = 0 To JSONArraySize( objet ) - 1
      texte.s = "[ "
      obj =GetJSONElement( objet, i ) 
      For ii = 0 To JSONArraySize( obj ) - 1
        texte = texte + GetJSONString( GetJSONElement( obj, ii ) )+" | "
      Next ii
      Debug texte + " ]"
      Debug ""
    Next i
  EndIf
EndIf
Sinon j'utilise l'Application Json Editor qui fonctionne très bien.