|
| Complex Variant structure |
| Iniciado por guest, 24,nov. 2017 06:49 - 4 respuestas |
| |
| | | |
|
| |
| Publicado el 24,noviembre 2017 - 06:49 |
Hi,
Is it possible to create complex Variant in windev? Would it be possible to create a variant with the following structure? e.g. Where "itinerary Days" was nested within the top element and could have a variable number of elements? I understand how to create a variant with a flat structure but how to create a more complex one? The end goal here is to write code to read data and create a variant structure and then use "VariantToJSON" to create the JSON structure.
{ "id": 540, "title": "14-Day Ultimate Namibia Safari", "itinerary_days": [{ "DayNbr": 1, "Title": "Day 1" }, { "DayNbr": 2, "Title": "Day 2" }, { "DayNbr": 3, "Title": "Day 3" }, { "DayNbr": 4, "Title": "Day 4" }, { "DayNbr": 5, "Title": "Day 5" }, { "DayNbr": 6, "Title": "Day 6" }, { "DayNbr": 7, "Title": "Day 7" }, { "DayNbr": 8, "Title": "Day 8" }, { "DayNbr": 9, "Title": "Day 9" }, { "DayNbr": 10, "Title": "Day 10" }, { "DayNbr": 11, "Title": "Day 11" }, { "DayNbr": 12, "Title": "Day 12" }, { "DayNbr": 13, "Title": "Day 13" }, { "DayNbr": 14, "Title": "Day 14" }] }
Thanks!
steve |
| |
| |
| | | |
|
| | |
| |
| Publicado el 24,noviembre 2017 - 10:58 |
Steve,
Something like this will work for holding the data. But I don't know if the JSON functions support this complexity....
ST_Days is Structure nDayNr is int sDayName is string END ST_TopLevel is Structure nID is int sTitle is string arrDays is array of ST_Days END stMyTopLevel is ST_TopLevel stMyTopLevel = new ST_TopLevel stDays is ST_Days stMyTopLevel.nID = 540 stMyTopLevel.sTitle = "14-Day Ultimate Namibia Safari" stDays.nDayNr = 1 stDays.sDayName = "Day 1" ArrarAdd(stMyTopLevel.arrDays,stDays) stDays.nDayNr = 2 stDays.sDayName = "Day 2" ArrayAdd(stMyTopLevel.arrDays,stDays) |
| |
| |
| | | |
|
| | |
| |
| Publicado el 24,noviembre 2017 - 11:43 |
Steve I do something slightly different...
PROCEDURE Test(ps_ID is string,ps_Title is string) //Structure = columns returned by query STR_arrItineryDays is Structure id is int title is string END //Array of the structure arrItineryDays is an array of STR_arrItineryDays //Procedure variables s_ID,s_JSONArray,s_JSONResult,s_Title is string v_ResArray is Variant //Formatted skeleton of the JSON document s_JSONDoc is string = [ } "id": %1 "title": %2 "itinery days": %3 } ] //Upadate variables from params received s_ID = ps_ID; s_Title = ps_Title //Run the query to return your itinery days HExecuteQuery(qryGetItineryDays,hQueryDefault,s_ID) IF HNbRec(qryGetItineryDays) > 0 THEN //Copy the query result set to the array FileToArray(arrItineryDays,qryGetItineryDays) //Copy the structure array to the variable array v_ResArray = arrItineryDays //Convert the variant array to JSON s_JSONArray = VariantToJSON(v_ResArray) //Update the JSON skeleton and copy to the result s_JSONResult = StringBuild(s_JSONDoc,s_ID,s_Title,s_JSONArray) END //Free the query allocation HFreeQuery(qryGetItineryDays) s_JSONResult will now contain the formatted document ready for your intended usage.
Coding does get a bit more complex where you require array within arrays but this can be overcome by declaring additional formatted skeleton strings and building the result set in sequence. |
| |
| |
| | | |
|
| | |
| |
| Publicado el 24,noviembre 2017 - 16:55 |
Thanks guys!
Derek, I really like your way for what I need to do. The actual task is going to be to present in JSON a top level table with 15 sub tables and there are upwards of 400 fields in all the tables combined. Being able to do a file to array on the table queries is going to save me a bat load of time.
Steve |
| |
| |
| | | |
|
| | |
| |
| Publicado el 26,noviembre 2017 - 19:54 |
Hi Steve,
vVar is Variant vVar.id=540 vVar.title="14-Day Ultimate Namibia Safari"
vDays is Variant FOR i=1 TO 14 vDays.DayNb=i vDays.Title="Day "+i END vVar.itinerary_days=vDays
Info(VariantToJSON(vVar,psdFormatting))
Hope it helps.
BR, Alen |
| |
| |
| | | |
|
| | | | |
| | |
|