IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Chargement de données à partir d'un fichier JSon


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Tchicken
    Homme Profil pro
    Responsable d'exploitation informatique
    Inscrit en
    Août 2017
    Messages
    108
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Responsable d'exploitation informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 108
    Par défaut Chargement de données à partir d'un fichier JSon
    Bonjour à tous,
    je coince sur le chargement de mes données à partir d'un fichier JSon téléchargé sur le Web :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    Imports VB = Microsoft.VisualBasic
    Imports System
    Imports System.IO
    Imports System.Net
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
     
    sub test()
            Dim JSonArrivee As JObject
            Dim Courses As String
            Dim DateCourse As String
            Dim MonURL As String
            Dim arrivee As String
            Dim Arrive As JToken
            Dim NGa As String
            Dim NP2 As String
            Dim NP3 As String
            Dim NP4 As String
            Dim NP5 As String
            Dim WebClient As New System.Net.WebClient()
            Dim UserAgent As String
     
            DateCourse = "13032020"
            MonURL = "https://offline.turfinfo.api.pmu.fr/rest/client/1/programme/" & DateCourse & "/R1/C1"
            Courses = My.Application.Info.DirectoryPath & "\Arrivees-R1C1-" & DateCourse & ".JSon"
            Try
                UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
                WebClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgent)
                WebClient.DownloadFile(MonURL, Courses)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            '***
            '*** Chargement du JSonObjet Arrivee
            '***
            Try
                JSonArrivee = JObject.Parse(File.ReadAllText(Courses))
                Arrive = JSonArrivee.GetValue("ordreArrivee")
            Catch ex As Exception
                MsgBox("Pas d'arrivée disponible pour la Réunion R1C1")
                exit sub
            End Try
            '***
            '*** Chargement de l'arrivée
            '***
            NGa = Arrive(0).ToString
            NP2 = Arrive(1).ToString
            NP3 = Arrive(2).ToString
            NP4 = Arrive(3).ToString
            NP5 = Arrive(4).ToString
    end sub
    Ca marche, mais j'obtient : "[" & vbCrLf & " 6" & vbCrLf & "]"
    Je voudrais que le 6

    j'ai aussi testé ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
                    NGa = Arrive.SelectToken(0).ToString
                    NP2 = Arrive.SelectToken(1).ToString
                    NP3 = Arrive.SelectToken(2).ToString
                    NP4 = Arrive.SelectToken(3).ToString
                    NP5 = Arrive.SelectToken(4).ToString
    Mais ca plante en exception

    URL du JSon : https://offline.turfinfo.api.pmu.fr/...1#ordreArrivee

    Je veux récupérer la valeur des index 0 à 4, les 5 premiers chevaux de l'arrivée.

    Merci de votre aide.

  2. #2
    Membre confirmé Avatar de Tchicken
    Homme Profil pro
    Responsable d'exploitation informatique
    Inscrit en
    Août 2017
    Messages
    108
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Responsable d'exploitation informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 108
    Par défaut
    J'ai trouvé une solution, c'est peut-être pas la meilleure, mais elle fonctionne ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
                    '***
                    '*** Chargement du JSonObjet Arrivee
                    '***
                    Try
                        JSonArrivee = JObject.Parse(File.ReadAllText(Courses))
                        Arrive = JArray.Parse(JSonArrivee.SelectToken("ordreArrivee").ToString)
                    Catch ex As Exception
                        'MsgBox("Pas d'arrivée disponible pour la Réunion " & RReunions.Fields("NumReunion").Value & " Course " & RCourses.Fields("NumCourse").Value)
                        'CoteRap.Text = "Erreur !"
                        GoTo CourseSuivante
                    End Try
                    '***
                    '*** Chargement de l'arrivée
                    '***
                    J = 1
                    For Each Arriv In Arrive
                        Select Case J
                            Case 1
                                NGa = Arriv(0).ToString()
                            Case 2
                                NP2 = Arriv(0).ToString()
                            Case 3
                                NP3 = Arriv(0).ToString()
                            Case 4
                                NP4 = Arriv(0).ToString()
                            Case 5
                                NP5 = Arriv(0).ToString()
                        End Select
                        J += 1
                    Next

  3. #3
    Membre Expert

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2010
    Messages
    2 067
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2010
    Messages : 2 067
    Par défaut
    Hello,
    tu as des sites pour générer tes classes à partir de json :
    https://www.jsonutils.com/

    Dans ton cas il me génère cette classe
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
        Public Class InfosJackpot
            Public Property miseBase As Integer
            Public Property tauxContribution As Integer
        End Class
     
        Public Class Pari
            Public Property codePari As String
            Public Property complement As Boolean
            Public Property spotAutorise As Boolean
            Public Property ordre As Boolean
            Public Property nbChevauxReglementaire As Integer
            Public Property combine As Boolean
            Public Property typePari As String
            Public Property miseBase As Integer
            Public Property enVente As Boolean
            Public Property audience As String
            Public Property reportable As Boolean
            Public Property infosJackpot As InfosJackpot
            Public Property valeursFlexiAutorisees As Integer()
        End Class
     
        Public Class PhotosArrivee
            Public Property heightSize As Integer
            Public Property widthSize As Integer
            Public Property url As String
            Public Property originalSize As Boolean
        End Class
     
        Public Class CommentaireApresCourse
            Public Property texte As String
            Public Property source As String
        End Class
     
        Public Class Penetrometre
            Public Property heureMesure As String
            Public Property intitule As String
            Public Property commentaire As String
        End Class
     
        Public Class Hippodrome
            Public Property codeHippodrome As String
            Public Property libelleCourt As String
            Public Property libelleLong As String
        End Class
     
        Public Class Example
            Public Property cached As Boolean
            Public Property arriveeDefinitive As Boolean
            Public Property departImminent As Boolean
            Public Property timezoneOffset As Integer
            Public Property numReunion As Integer
            Public Property numOrdre As Integer
            Public Property numExterne As Integer
            Public Property heureDepart As Long
            Public Property libelle As String
            Public Property libelleCourt As String
            Public Property montantPrix As Integer
            Public Property parcours As String
            Public Property distance As Integer
            Public Property distanceUnit As String
            Public Property corde As String
            Public Property discipline As String
            Public Property specialite As String
            Public Property categorieParticularite As String
            Public Property conditionAge As String
            Public Property conditionSexe As String
            Public Property nombreDeclaresPartants As Integer
            Public Property grandPrixNationalTrot As Boolean
            Public Property numSocieteMere As Integer
            Public Property pariMultiCourses As Boolean
            Public Property pariSpecial As Boolean
            Public Property montantTotalOffert As Integer
            Public Property montantOffert1er As Integer
            Public Property montantOffert2eme As Integer
            Public Property montantOffert3eme As Integer
            Public Property montantOffert4eme As Integer
            Public Property montantOffert5eme As Integer
            Public Property conditions As String
            Public Property numCourseDedoublee As Integer
            Public Property paris As Pari()
            Public Property typePiste As String
            Public Property statut As String
            Public Property categorieStatut As String
            Public Property dureeCourse As Integer
            Public Property participants As Object()
            Public Property ecuries As Object()
            Public Property photosArrivee As PhotosArrivee()
            Public Property commentaireApresCourse As CommentaireApresCourse
            Public Property penetrometre As Penetrometre
            Public Property rapportsDefinitifsDisponibles As Boolean
            Public Property isArriveeDefinitive As Boolean
            Public Property isDepartImminent As Boolean
            Public Property isDepartAJPlusUn As Boolean
            Public Property cagnottes As Object()
            Public Property pronosticsExpires As Boolean
            Public Property replayDisponible As Boolean
            Public Property hippodrome As Hippodrome
            Public Property epcPourTousParis As Boolean
            Public Property courseTrackee As Boolean
            Public Property formuleChampLibreIndisponible As Boolean
            Public Property ordreArrivee As Integer()()
            Public Property hasEParis As Boolean
        End Class
    plus que Faire un Json.Deserialize<Example>(jsontext);.

    et ça roule

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 31/08/2006, 16h31
  2. Demande d'aide sur les regexp
    Par Uld dans le forum Langage
    Réponses: 1
    Dernier message: 18/08/2006, 22h15
  3. [VB6] Demande d'aide sur un programme !
    Par Lucas42 dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 16/06/2006, 11h49
  4. Demande d'aide sur XPDL (XML Process Definition Language)
    Par Bebert71 dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 17/02/2006, 09h23
  5. demande d'aide sur samba
    Par marcoss dans le forum Développement
    Réponses: 5
    Dernier message: 04/12/2003, 19h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo