Return 多个 Json 回复
Return multiple Json responses
我已经构建了这个 API returns JSON 来自 Google 的响应将 API 到 store/save 的响应放入数据库并且因为这个代码示例显示它是 PlaceId 列表,所以我写了一个 For 循环来循环每个 PlaceId 和 return 它们然后到下一个点,即 post 它们到数据库,
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);
}
但是当我测试时会发生什么,它只是 returnFor 循环的最后一个 PlaceId 响应。
关于如何 return 将它们全部排列并保存的任何想法?
Any ideas on how to return them all to maybe array and save them?
使用列表可能更容易:
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);
// ^
用脱字符号下划线的新位 ^
但是我想你可以使用一个数组。毕竟你知道你要下载多少个地方..
var responsesFromServer = new string[PlaceId.Count()];
for (int i = 0; i < PlaceId.Count(); i++)
{
...
responsesFromServer[i] = await reader.ReadToEndAsync();
}
return new JsonResult(responsesFromServer);
我已经构建了这个 API returns JSON 来自 Google 的响应将 API 到 store/save 的响应放入数据库并且因为这个代码示例显示它是 PlaceId 列表,所以我写了一个 For 循环来循环每个 PlaceId 和 return 它们然后到下一个点,即 post 它们到数据库,
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);
}
但是当我测试时会发生什么,它只是 returnFor 循环的最后一个 PlaceId 响应。 关于如何 return 将它们全部排列并保存的任何想法?
Any ideas on how to return them all to maybe array and save them?
使用列表可能更容易:
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);
// ^
用脱字符号下划线的新位 ^
但是我想你可以使用一个数组。毕竟你知道你要下载多少个地方..
var responsesFromServer = new string[PlaceId.Count()];
for (int i = 0; i < PlaceId.Count(); i++)
{
...
responsesFromServer[i] = await reader.ReadToEndAsync();
}
return new JsonResult(responsesFromServer);