Importation jason (champ null)
Bonjour Forum,
J'avais ouvert un post il y a quelque temps, et pierrep56 m'avait bien aidé.
J'aurais besoin d'un petit ajustement sur mon fichier en PJ, comment fait on pour palier au cas ou un champ est vide ? Par exemple dans le lien du jason un champ à la valeur null, donc soit on met un "On Error Resume Next" et ça ne fonctionne pas (pas d'importation) soit on ne met rien et ça affiche avec une erreur dû au champ "null".
J'ai volontairement mis sur le hometeam sur la première équipe la valeur null dans le json.
Voilà vôtre sera la bienvenue.
Bah comment ça se fait ? Avast il dit rien chez moi
Je met le code ici
Option Explicit
' ***********************************************************************
' ***** *****
' ***** CODE PierreP56 : http://tatiak.canalblog.com/ *****
' ***** *****
' ***********************************************************************
Public TCode As Variant
Sub Bourne()
Dim DataSet As Object, Elem As Object
Dim Site As String, i As Long
Dim Res As Variant ', T As Variant
Site = "http://simple.gagnant.place.free.fr/essai2.js"
Set DataSet = VBA.CallByName(oRecordSet(Site), "data", VbGet)
ReDim Res(14, 0)
For i = 0 To UBound(TCode) - 1
ReDim Preserve Res(14, i)
Set Elem = VBA.CallByName(DataSet, TCode(i), VbGet)
Res(0, i) = TCode(i)
Res(1, i) = Split(VBA.CallByName(Elem, "time", VbGet))(0)
Res(2, i) = Split(VBA.CallByName(Elem, "time", VbGet))(1)
Res(3, i) = Elem.hometeam
Res(4, i) = Elem.awayteam
Res(5, i) = Elem.country
Res(6, i) = VBA.CallByName(Elem.first, "1", VbGet)
Res(7, i) = VBA.CallByName(Elem.first, "X", VbGet)
Res(8, i) = VBA.CallByName(Elem.first, "2", VbGet)
Res(9, i) = VBA.CallByName(Elem.last, "1", VbGet)
Res(10, i) = VBA.CallByName(Elem.last, "X", VbGet)
Res(11, i) = VBA.CallByName(Elem.last, "2", VbGet)
Set Elem = VBA.CallByName(Elem, "profit%", VbGet)
Res(12, i) = VBA.CallByName(Elem, "1", VbGet)
Res(13, i) = VBA.CallByName(Elem, "X", VbGet)
Res(14, i) = VBA.CallByName(Elem, "2", VbGet)
Next i
Res = Application.Transpose(Res)
Sheets("Feuil1").Range("A2").Resize(UBound(Res, 1), UBound(Res, 2)) = Res
'T = Array("Code", "Date", "Heure", "hometeam", "awayteam", "country", _
"First_1", "First_X", "First_2_", "Last_1", "Last_X", "Last_2")
'Sheets("Feuil1").Range("A1").Resize(1, UBound(T, 1) + 1) = T
Set Elem = Nothing
Set DataSet = Nothing
End Sub
Function oRecordSet(Ttk As String) As Object
Dim ScriptControl As Object, Html As Object, Obj As Object, S As String, i As Integer
Dim reg As VBScript_RegExp_55.RegExp
Dim Match As VBScript_RegExp_55.Match
Dim Matches As VBScript_RegExp_55.MatchCollection
Set ScriptControl = CreateObject("MSScriptControl.ScriptControl")
ScriptControl.Language = "JScript"
Set Html = CreateObject("MSXML2.XMLHTTP")
With Html
.Open "GET", Ttk, False
.send
S = .responsetext
Set Obj = ScriptControl.Eval("(" & S & ")")
Set reg = New VBScript_RegExp_55.RegExp
reg.Pattern = "\d{7}-\d"
reg.Global = True
i = 0
ReDim TCode(i)
Set Matches = reg.Execute(S)
For Each Match In Matches
S = Match.Value
If Idx_T(TCode, S) < 0 Then
TCode(i) = S
i = i + 1
ReDim Preserve TCode(i)
End If
Next Match
End With
Set oRecordSet = Obj
Set Obj = Nothing
End Function
Function Idx_T(Ttk As Variant, V As Variant) As Integer
Dim i As Long
Idx_T = LBound(Ttk) - 1
For i = LBound(Ttk) To UBound(Ttk, 1)
If CStr(V) = CStr(Ttk(i)) Then Idx_T = i
Next i
End Function
Re,
Je ne sais pas !...
C'est une première pour moi et de plus sur le forum !...
Cdlt.
Avec 11585 messages
Sinon t'as la soluce a mon petit problème ?