Il giorno sabato 12 gennaio 2013 04:34:25 UTC+1,
androi...@gmail.com ha scritto:
> r ... sei un mostro (nel buon senso!) :-)
di certo non "del" buon senso :-D
> devo ancora provare il tuo metodo json e quello si sarebbe il passo verso la
> perfezione!
intanto ho trovato le specifiche anche in italiano e scritte bene! quasi un miracolo in questo campo :-)
http://json.org/json-it.html
poi ieri ho condiviso le routine con il mio amico ungherese Gabor che ha trovato molto interessante l'argomento (e non ne dubitavo :-) e ovviamente ci ha messo del suo :-) così ad esempio io non avevo considerato che i numeri potessero essere anche negativi o in formato scientifico ... poi ha giustamente fatto notare è inutile creare l'oggetto regexp tutte quelle volte ... meglio farlo una volta all'inizio e basta, così si risparmia un bel po' di tempo ... comunque la faccio breve ed ecco il nuovo codice ... manca ancora la gestione dei caratteri unicode ... appena riesco ...
ciao
r
Option Explicit
'
http://json.org/json-it.html
Private RE As Object
Sub testRoberto0()
Dim sJSON As String
sJSON = TextFromURL("
http://bitcoincharts.com/t/markets.json")
If Left(sJSON, 2) <> "[{" Then
Debug.Print "*** No data found"
Debug.Print sJSON
End
End If
Write_JSON_on_Range sJSON, ActiveWorkbook.Worksheets.Add.[a1]
End Sub
Public Function TextFromURL(myURL As String)
Dim myIE As Object
Const READYSTATE_COMPLETE As Long = 4
Set myIE = CreateObject("InternetExplorer.Application")
'myIE.Visible = True
myIE.navigate myURL
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
TextFromURL = myIE.document.body.innerHTML
myIE.Quit
Set myIE = Nothing
End Function
Private Sub Write_JSON_on_Range(sJSON As String, rng As Excel.Range)
Dim M As Object, SM As Object ', RE As Object
Dim bFirstRow As Boolean, r As Long
If RE Is Nothing Then
Set RE = CreateObject("vbscript.regexp")
End If
RE.ignorecase = True
RE.Global = True
bFirstRow = True
RE.Pattern = "\{[^}]+}"
If RE.test(sJSON) Then
Set M = RE.Execute(sJSON)
For Each SM In M
If bFirstRow Then
Write_JSON_Row SM.Value, rng.Offset(r), bFirstRow
bFirstRow = False
r = r + 1
Else
Write_JSON_Row SM.Value, rng.Offset(r), bFirstRow
End If
r = r + 1
Next
End If
End Sub
Private Sub Write_JSON_Row(sJSON As String, rng As Excel.Range, bFirstRow As Boolean)
Dim M As Object, SM As Object, SB As Object ', RE As Object
Dim c As Long
'Set RE = CreateObject("vbscript.regexp")
'RE.ignorecase = True
'RE.Global = True
RE.Pattern = "\s*""((?:\\""|[^""])*)""\s*:\s*(null|true|false|[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|""(?:\\""|[^""])*"")\s*"
If RE.test(sJSON) Then
Set M = RE.Execute(sJSON)
For Each SM In M
Set SB = SM.SubMatches
If bFirstRow Then
rng.Offset(0, c) = Conv_JSON_String_Value(SB(0))
rng.Offset(1, c) = Conv_JSON_String_Value(Conv_JSON_Value(SB(1)))
Else
rng.Offset(0, c) = Conv_JSON_String_Value(Conv_JSON_Value(SB(1)))
End If
c = c + 1
Next
End If
End Sub
Private Function Conv_JSON_Value(sValue As String)
Dim M As Object, SM As Object, SB As Object ', RE As Object
Dim c As Long
'Set RE = CreateObject("vbscript.regexp")
'RE.ignorecase = True
'RE.Global = True
RE.Pattern = "(null)|""((?:\\""|[^""])*)"""
If RE.test(sValue) Then
Set M = RE.Execute(sValue)
Set SB = M(0).SubMatches
If Len(SB(0)) Then
'-> Conv_JSON_Value = Empty
Else
Conv_JSON_Value = SB(1)
End If
Else
Conv_JSON_Value = sValue
End If
End Function
' string = quotation-mark *char quotation-mark
'
' char = unescaped /
' escape (
' %x22 / ; " quotation mark U+0022
' %x5C / ; \ reverse solidus U+005C
' %x2F / ; / solidus U+002F
' %x62 / ; b backspace U+0008
' %x66 / ; f form feed U+000C
' %x6E / ; n line feed U+000A
' %x72 / ; r carriage return U+000D
' %x74 / ; t tab U+0009
' %x75 4HEXDIG ) ; uXXXX U+XXXX
'
' escape = %x5C ; \
'
' quotation-mark = %x22 ; "
'
' unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
Private Function Conv_JSON_String_Value(sValue As String)
Dim c As Long
c = InStr(sValue, "\")
If c > 0 Then
sValue = Replace(sValue, "\""", """")
sValue = Replace(sValue, "\\", "\")
sValue = Replace(sValue, "\/", "/")
sValue = Replace(sValue, "\b", Chr(8))
sValue = Replace(sValue, "\f", Chr(12))
sValue = Replace(sValue, "\n", Chr(10))
sValue = Replace(sValue, "\r", Chr(13))
sValue = Replace(sValue, "\t", Chr(9))
'TODO: RegExp for Unicode
'sValue = Replace(sValue, "\u????", Chr(????))
Else
Conv_JSON_String_Value = sValue
End If
End Function