Condividi:        

[Excel] Intterrogazione REST e gestione JSON

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

[Excel] Intterrogazione REST e gestione JSON

Postdi Dylan666 » 14/11/22 10:47

Ciao a tutti,
devo provare a interrogare un webservice da Excel che mi restituirà un JSON.
Dovrò gestire un POST con autenticazione, header personalizzati ecc, ma ho deciso di partire da un esempio semplice.
Ho trovato questo:
https://marquesfernandes.com/en/develop ... son/?amp=1

Ho fatto quanto descritto (almeno credo) ma su questa riga:

Codice: Seleziona tutto
For Each pokemon InJsonObject("results")


ottengo un errore di sintassi.
Dove posso aver sbagliato?
Avatar utente
Dylan666
Moderatore
 
Post: 39988
Iscritto il: 18/11/03 16:46

Sponsor
 

Re: [Excel] Intterrogazione REST e gestione JSON

Postdi Anthony47 » 14/11/22 17:38

Non hai sbagliato, ci sono proprio errori nel codice che ti hanno detto di copiare..

C'e' un errore di sintassi sulla linea che ho "commentato"
Codice: Seleziona tutto
'For Each pokemon InjsonObject("results")
For Each pokemon In jsonObject("results")

Qui invece, si e' "sconfuso":
Codice: Seleziona tutto
'Set ObjectJSon = JsonConverter.ParseJson(json)
Set jsonObject = JsonConverter.ParseJson(json)


Ri-prova...
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: [Excel] Intterrogazione REST e gestione JSON

Postdi Dylan666 » 14/11/22 18:02

Grazie veramente, lo spazio mancante dopo "In" non lo avevo proprio visto e nemmeno l'errore nel nome della variabile richiamata...
Risolto!

Posto il codice corretto:
Codice: Seleziona tutto
Sub listPokemons()
Dim json As String
Dim jsonObject As Object, item As Object
Dim i As Long
Dim ws As Worksheet
Dim objHTTP As Object

'We selected our results sheet
Set ws = Worksheets("results")

'We create our request object and send
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
Url = "https://pokeapi.co/api/v2/pokemon"
objHTTP.Open "GET", Url, False
objHTTP.Send
strResult = objHTTP.responseText
json = strResult

Set objectJson = JsonConverter.ParseJson(json)

'We create the header cells
ws.Cells(1, 1) = "name"
ws.Cells(1, 2) = "link"

'We loop the results property of the API response
'We will start the counter on line 2
i = 2
For Each pokemon In objectJson("results")
    ws.Cells(i, 1) = pokemon("name")
    ws.Cells(i, 2) = pokemon("url")
    i = i + 1
Next

End Sub

Avatar utente
Dylan666
Moderatore
 
Post: 39988
Iscritto il: 18/11/03 16:46

Re: [Excel] Intterrogazione REST e gestione JSON

Postdi Anthony47 » 18/11/22 20:14

Ho inserito per errore in altra tua discussione questo messaggio: viewtopic.php?f=26&t=112670&p=662281#p662281

Ovviamente era da riferito a QUESTA discussione...
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: [Excel] Intterrogazione REST e gestione JSON

Postdi Dylan666 » 18/11/22 21:07

Grazie ma non ti preoccupare, il vero webservice che interrogo risponde sia in JSON che in XML ed ho optato per quest'ultimo.
Nel leggere la risposta però certe volte il tag che cerco non c'è e sto cercando di gestire questa eccezione ma con poco successo.
Posso scocciarti anche su questo?
Avatar utente
Dylan666
Moderatore
 
Post: 39988
Iscritto il: 18/11/03 16:46

Re: [Excel] Intterrogazione REST e gestione JSON

Postdi Anthony47 » 19/11/22 00:16

Chiedere si puo' sempre, non e' sempre garantita una risposta soddisfacente :D
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea


Torna a Applicazioni Office Windows


Topic correlati a "[Excel] Intterrogazione REST e gestione JSON":


Chi c’è in linea

Visitano il forum: Nessuno e 65 ospiti