How to build foreach loop based on a web request

Hi,

I have a scene that do a GET web request and get X responses back. I take that response and add it to a textbox as text. That part works. But instead of adding all text to a textbox I would like to do a “foreach” loop, and read GPS long/lat from each response and run a “add marker to mapview” command. 

How do I create a foreach loop in Corona SDK? I have found a number of for 1 to 20 examples, but I never now number of events I get back from the web service.

Thanks 

network.request( "http://XX.XX.XX.XX/api/v2/cstri-sitrap/\_table/cstri.sitrap?filter=alliance%20%3D%20NAF", "GET", handleResponse, params )

local json = require( "json" ) local mime = require( "mime" ) local function handleResponse( event )     if not event.isError then         local response = json.decode( event.response )         defaultBox.text = event.response     else         print( "######## Error ############" )     end     return end

Use a while loop if you do not know the length of the request

Thanks for your reply.

I have tried to find a example of this kind of loop togheter with network.request. Do you have a example? Thanks 

syntax is

while condition do --code here end

or if response is a table object you could do

for i = 1, #response do --code here end

Hi,

Assuming your response is being returned as a collection of objects/records, perhaps like this (json):

{ {"msg":"some text 1", "lat":100, "long":100}, {"msg":"some text 2", "lat":200, "long":200} }

Then you can do like so:

local records = json.decode( evt.response ) for idx, record in ipairs( records ) do   print( record.msg, record.lat, record.long ) end

If the response is just one blob of text though then you would need to do some more intricate parsing.

Hope that helps,

-dev

Use a while loop if you do not know the length of the request

Thanks for your reply.

I have tried to find a example of this kind of loop togheter with network.request. Do you have a example? Thanks 

syntax is

while condition do --code here end

or if response is a table object you could do

for i = 1, #response do --code here end

Hi,

Assuming your response is being returned as a collection of objects/records, perhaps like this (json):

{ {"msg":"some text 1", "lat":100, "long":100}, {"msg":"some text 2", "lat":200, "long":200} }

Then you can do like so:

local records = json.decode( evt.response ) for idx, record in ipairs( records ) do   print( record.msg, record.lat, record.long ) end

If the response is just one blob of text though then you would need to do some more intricate parsing.

Hope that helps,

-dev