{"id":37049,"date":"2024-05-08T21:23:18","date_gmt":"2024-05-08T19:23:18","guid":{"rendered":"https:\/\/help.qualiware.it\/qw-help\/?p=37049"},"modified":"2024-05-08T21:23:18","modified_gmt":"2024-05-08T19:23:18","slug":"action-link-per-la-restituzione-di-un-flusso-json-contenente-il-risultato-di-una-query","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/action-link-per-la-restituzione-di-un-flusso-json-contenente-il-risultato-di-una-query\/","title":{"rendered":"Action link per la restituzione di un flusso JSON contenente il risultato di una query"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\">' Il seguente codice pu\u00f2 essere usato come esempio per realizzare un action link che restituire un flusso JSON che contiene il risultato di una query \r\n' Come si pu\u00f2 vedere, viene usato un oggetto List che contiene un oggetto Dictionary il cui primo elemento \u00e8 il nome del campo e il secondo il valore\r\n' L'oggetto List viene poi serializzato nell'ultima riga. \r\n' L'action link accetta due parametri nel payload: Data_Inizio_Evento e Data_Fine_Evento\r\ndim ret_obj\r\n\r\nDim obj As Newtonsoft.Json.linq.JObject\r\nDim jsonr as string\r\nPage.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin)\r\nUsing receiveStream As System.IO.Stream = Page.Request.InputStream\r\n   Using readStream As System.IO.StreamReader = New System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8)\r\n      jsonr = readStream.ReadToEnd()\r\n   End Using\r\nEnd Using\r\n\r\nIf empty(jsonr)\r\n    ret_obj=New With {.Result = \"KO\",.Reason=\"Parametri non definiti\"}\r\n    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)\r\nEnd If\r\n\r\nobj=Newtonsoft.Json.linq.JObject.Parse(jsonr)  \r\n\r\ndim Data_Inizio_Evento as Date\r\nif not obj(\"Data_Inizio_Evento\") is nothing\r\n    Data_Inizio_Evento=ctype(obj(\"Data_Inizio_Evento\"),Newtonsoft.Json.Linq.JValue).Value\r\nElse\r\n    ret_obj=New With {.Result = \"KO\",.Reason=\"Parametro Data_Inizio_Evento non specificato\"}\r\n    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)\r\nEnd If\r\n\r\ndim Data_Fine_Evento as Date\r\nif not obj(\"Data_Fine_Evento\") is nothing\r\n    Data_Fine_Evento=ctype(obj(\"Data_Fine_Evento\"),Newtonsoft.Json.Linq.JValue).Value\r\nElse\r\n    ret_obj=New With {.Result = \"KO\",.Reason=\"Parametro Data_Fine_Evento non specificato\"}\r\n    return newtonsoft.Json.jsonconvert.SerializeObject(ret_obj)\r\nEnd If\r\n\r\nDim q As New QWTable\r\nq.Database = DB\r\nq.SQL = \"select \r\n            TIPO_CONT+NUMERO as idQuestionario,\r\n            CUSTOM22 as Codice_Evento,\r\n            CUSTOM2 as Tipo_evento,\r\n            CUSTOM16 as Tipo_Location,\r\n            DATE4 as Data_Fine_Pubblicazione,\r\n            CUSTOM15 as Cooperativa,\r\n            CUSTOM9 as EAN_Prodotto,\r\n            left(CUSTOM10,7) as Codice_Prodotto,\r\n            Substring(CUSTOM10,11,200) as Descrizione_Prodotto,\r\n            CUSTOM20 as Fornitore,\r\n            CUSTOM4 as Codice_Segmento_Clsmkt,\r\n            CUSTOM11 as Mondo,\r\n            CUSTOM5 as Reparto,\r\n            CUSTOM6 as Categoria,\r\n            CUSTOM7 as Subcategoria,\r\n            CUSTOM8 as Segmento,\r\n            CUSTOM14 as Numero_Questionario\r\n         from DOCUMENT where TIPO='QUESTAS' and CUSTOM2 in ('ADS COOPERATIVE','ADS ONLINE','ADS AGENZIE') and DATE4 between :DA_DATA and :A_DATA\"\r\nq.params(\"DA_DATA\")=Data_Inizio_Evento\r\nq.params(\"A_DATA\")=Data_Fine_Evento\r\nq.RequestLive=false\r\nq.active = True\r\n\r\nDim rows As New System.Collections.Generic.List(Of System.Collections.Generic.Dictionary(Of String, Object))()\r\nDim row As System.Collections.Generic.Dictionary(Of String, Object) \r\n\r\nWhile not q.rowset.endofset\r\n    dim numdoc as string\r\n    UnPackCode(\"IS\",q.rowset.fields(\"Numero_Questionario\").value,numdoc)\r\n\r\n    row = New System.Collections.Generic.Dictionary(Of String, Object)()  \r\n\r\n    dim i as integer\r\n    dim field as string\r\n    dim val as object\r\n    for i=1 to q.rowset.fields.size\r\n        field=q.Rowset.fields(i).fieldname\r\n        if field&lt;&gt;\"Numero_Questionario\"\r\n            val=q.rowset.fields(i).value\r\n\r\n            row.Add(field, val)\r\n        End If\r\n    next\r\n                \r\n    rows.add(row)\r\n\r\n    q.rowset.next()\r\nEnd While\r\n\r\nSCDOMQ.active=false\r\nq.active=false\r\n\r\nWrite_LogDoc(DB.QWSession,\"**\",\"ACTIONLINK\",1,\"BI - Questionari da data inizio evento: \"+dtoc(Data_Inizio_Evento)+\" a data fine evento: \"+dtoc(Data_Fine_Evento),\"###\")\r\n\r\nreturn newtonsoft.Json.jsonconvert.SerializeObject(rows)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; Il seguente codice pu\u00f2 essere usato come esempio per realizzare un action link che restituire un flusso JSON che contiene il risultato di una query &#8216; Come si pu\u00f2 vedere, viene usato un oggetto List che contiene un oggetto Dictionary il cui primo elemento \u00e8 il nome del campo e il secondo il valore&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[263],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/37049"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=37049"}],"version-history":[{"count":1,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/37049\/revisions"}],"predecessor-version":[{"id":37050,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/37049\/revisions\/37050"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=37049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=37049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=37049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}