Skip to content

ARRAY IN JSON VBA #244

Open
Open
@d4ta4nalyst

Description

@d4ta4nalyst

hello, my knowledge as a developer is not that good, but by collecting code on the web, I was able to adapt the content to what I really wanted.

The question is that I am specifically working with a remote version of Limesurvey (a survey software). But apparently, the software, to make remote requests uses json. The case is that I try to show certain attributes, which are an array and I don't know how to proceed to do this adapted to VBA...

If someone has some idea and can tell me how, even if it's the introduction to learn how to do this, since I've really been several weeks and I can't get this...

I show you the code in json:

{
"method":"export_responses",
"params": [
"U053k2PTgDSifiZKkfZUQ5v6wlrbfsnt",
"433682",
"csv",
null,
"complete",
null,
null,
"1",
"30",
["firstname", "lastname"]
],
"id":1
}

I show you the code in VBA-JSON:

Sub export_limesurvey()
Dim key As String
Dim limeuser As String, limepass As String, limeurl As String, URL As String
Dim jsonText As String, jsonObject As Object
Dim SurveyID As String, DocumentType As String, LanguageCode As String, CompletionStatus As String, HeadingType As String, ResponseType As String, FromResponseID As String, ToResponseID As String
Dim Fields As VBA.Collection
Dim export64 As String, export64Decoded As String

limeurl = "URL CENSORED"
limeuser = "CENSORED"
limepass = "CENSORED"
SurveyID = Worksheets("Hoja2").Range("A6").Value 'Valor entre comillas para indicar ID encuesta a exportar (He hecho referencia a celda)
DocumentType = "csv" 'Tipo de formato en el que se desea exportrar (pdf,csv,xls,doc,json)
LanguageCode = "es" 'OPCIONAL: indicar idioma de exportación (solo si la encuesta está en ese idioma)
CompletionStatus = "complete" 'Exportar encuestas completas, incompletas o todas (complete,incomplete,all)
HeadingType = "full" 'Impresión de los titulos de las encuestas; códigos, todo o abreviados (code,full,abbreviated)
ResponseType = "long" 'Mostrar los valores de las preguntas de selección; corto para código y largo para mostrar valores (short,long)
FromResponseID = "0" 'Indicar desde el primer número de respuesta en la encuesta desde el que se desea exportar
ToResponseID = "10" 'Indicar hasta el último número de respuesta en la encuesta hasta el que se desea exportar
Fields = Array("firstname", "lastname")

'Inicialización
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = limeurl + "/admin/remotecontrol"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-type", "application/json"

'Establecer acceso (get_session_key)
sendtext = "{""method"":""get_session_key"",""params"": [""" + limeuser + """,""" + limepass + """],""id"": 1}"
objHTTP.Send (sendtext)
jsonText = objHTTP.responseText
Set jsonObject = JsonConverter.ParseJson(jsonText)
key = jsonObject("result")

Where I think the error is here. Say the code works without adding the "fields" array:

'Exportar respuestas (export_responses)
sendtext = "{""method"":""export_responses"",""params"": [""" + key + """,""" + SurveyID + """,""" + DocumentType + """,""" + LanguageCode + """,""" + CompletionStatus + """,""" + HeadingType + """,""" + ResponseType + """,""" + FromResponseID + """,""" + ToResponseID + """,""" + [""" + Fields + """] + """],""id"": 1}"
objHTTP.Send (sendtext)
jsonText = objHTTP.responseText
Set jsonObject = JsonConverter.ParseJson(jsonText)
export64 = jsonObject("result")

Thanks for reading...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions