简体   繁体   中英

Return multiple Json responses

I have built this API which returns JSON responses from Google place API to store/save it to the database and as this code sample shows it's List of PlaceId so I've written a For loop to loop every PlaceId and return them all then to the next point which is to post them to the database,

    public class portal_teilnehmerController : ControllerBase
{
    private readonly _0046696KContext _context;
    private const string apiKey = @"apiKey";
    private const string fields = "&fields=address_component,rating,reviews,user_ratings_total,website";
    WebRequest request;
    WebResponse response;
    Stream data;
    StreamReader reader;
    private Task<string> responseFromServer;
    private string[] JsonResponses = { };

    public portal_teilnehmerController(_0046696KContext context)
    {
        _context = context;
    }
    [HttpGet]
    [Produces("application/json")]
    public async Task<JsonResult> Getportal_teilnehmerByPlaceId()
    {
        var PlaceId = await _context.portal_teilnehmer.Select(x => x.PlaceId).ToListAsync();

        if (PlaceId == null)
        {
            throw new InvalidOperationException("This Portal ID not found, please be assure of your portal ID");
        }

        for (int i = 0; i < PlaceId.Count(); i++)
        {

            string url = @"https://maps.googleapis.com/maps/api/place/details/json?place_id=" + (PlaceId[i]) + (fields) + (apiKey);

            request = WebRequest.Create(url);

            response = await request.GetResponseAsync();

            data = response.GetResponseStream();

            reader = new StreamReader(data);

            string timeStamp = GetTimestamp(DateTime.Now);

            responseFromServer = reader.ReadToEndAsync();
        }
        return new JsonResult(await responseFromServer);
    }

but what happens when I test, it just returns me the last PlaceId response of the For loop. Any ideas on how to return them all to maybe array and save them?

Any ideas on how to return them all to maybe array and save them?

It's probably easier to use a List:

    var responsesFromServer = new List<string>();
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    for (int i = 0; i < PlaceId.Count(); i++)
    {

        string url = @"https://maps.googleapis.com/maps/api/place/details/json?place_id=" + (PlaceId[i]) + (fields) + (apiKey);

        request = WebRequest.Create(url);

        response = await request.GetResponseAsync();

        data = response.GetResponseStream();

        reader = new StreamReader(data);

        string timeStamp = GetTimestamp(DateTime.Now);

        responsesFromServer.Add(await reader.ReadToEndAsync());
        //      ^          ^^^^^^^^^^                        ^
    }
    return new JsonResult(responsesFromServer);
    //                            ^

New bits underlined with a caret ^

But you could use an array, I suppose.. After all you do know how many places you're going to download..

    var responsesFromServer = new string[PlaceId.Count()];
    for (int i = 0; i < PlaceId.Count(); i++)
    {
        ...
        responsesFromServer[i] = await reader.ReadToEndAsync();
    }
    return new JsonResult(responsesFromServer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM