Action link per la restituzione di un flusso JSON contenente il risultato di una query

' Il seguente codice può essere usato come esempio per realizzare un action link che restituire un flusso JSON che contiene il risultato di una query 
' Come si può vedere, viene usato un oggetto List che contiene un oggetto Dictionary il cui primo elemento è il nome del campo e il secondo il valore
' L'oggetto List viene poi serializzato nell'ultima riga. 
' L'action link accetta due parametri nel payload: Data_Inizio_Evento e Data_Fine_Evento
dim ret_obj

Dim obj As Newtonsoft.Json.linq.JObject
Dim jsonr as string
Page.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin)
Using receiveStream As System.IO.Stream = Page.Request.InputStream
   Using readStream As System.IO.StreamReader = New System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8)
      jsonr = readStream.ReadToEnd()
   End Using
End Using

If empty(jsonr)
    ret_obj=New With {.Result = "KO",.Reason="Parametri non definiti"}
    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)
End If

obj=Newtonsoft.Json.linq.JObject.Parse(jsonr)  

dim Data_Inizio_Evento as Date
if not obj("Data_Inizio_Evento") is nothing
    Data_Inizio_Evento=ctype(obj("Data_Inizio_Evento"),Newtonsoft.Json.Linq.JValue).Value
Else
    ret_obj=New With {.Result = "KO",.Reason="Parametro Data_Inizio_Evento non specificato"}
    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)
End If

dim Data_Fine_Evento as Date
if not obj("Data_Fine_Evento") is nothing
    Data_Fine_Evento=ctype(obj("Data_Fine_Evento"),Newtonsoft.Json.Linq.JValue).Value
Else
    ret_obj=New With {.Result = "KO",.Reason="Parametro Data_Fine_Evento non specificato"}
    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)
End If

Dim q As New QWTable
q.Database = DB
q.SQL = "select 
            TIPO_CONT+NUMERO as idQuestionario,
            CUSTOM22 as Codice_Evento,
            CUSTOM2 as Tipo_evento,
            CUSTOM16 as Tipo_Location,
            DATE4 as Data_Fine_Pubblicazione,
            CUSTOM15 as Cooperativa,
            CUSTOM9 as EAN_Prodotto,
            left(CUSTOM10,7) as Codice_Prodotto,
            Substring(CUSTOM10,11,200) as Descrizione_Prodotto,
            CUSTOM20 as Fornitore,
            CUSTOM4 as Codice_Segmento_Clsmkt,
            CUSTOM11 as Mondo,
            CUSTOM5 as Reparto,
            CUSTOM6 as Categoria,
            CUSTOM7 as Subcategoria,
            CUSTOM8 as Segmento,
            CUSTOM14 as Numero_Questionario
         from DOCUMENT where TIPO='QUESTAS' and CUSTOM2 in ('ADS COOPERATIVE','ADS ONLINE','ADS AGENZIE') and DATE4 between :DA_DATA and :A_DATA"
q.params("DA_DATA")=Data_Inizio_Evento
q.params("A_DATA")=Data_Fine_Evento
q.RequestLive=false
q.active = True

Dim rows As New System.Collections.Generic.List(Of System.Collections.Generic.Dictionary(Of String, Object))()
Dim row As System.Collections.Generic.Dictionary(Of String, Object) 

While not q.rowset.endofset
    dim numdoc as string
    UnPackCode("IS",q.rowset.fields("Numero_Questionario").value,numdoc)

    row = New System.Collections.Generic.Dictionary(Of String, Object)()  

    dim i as integer
    dim field as string
    dim val as object
    for i=1 to q.rowset.fields.size
        field=q.Rowset.fields(i).fieldname
        if field<>"Numero_Questionario"
            val=q.rowset.fields(i).value

            row.Add(field, val)
        End If
    next
                
    rows.add(row)

    q.rowset.next()
End While

SCDOMQ.active=false
q.active=false

Write_LogDoc(DB.QWSession,"**","ACTIONLINK",1,"BI - Questionari da data inizio evento: "+dtoc(Data_Inizio_Evento)+" a data fine evento: "+dtoc(Data_Fine_Evento),"###")

return newtonsoft.Json.jsonconvert.SerializeObject(rows)