[英]problem with returning some lists in Json : ASP.NET Core
我正在创建一个API,该API从MySql数据库返回一些结果。 我有一个包含2行和一些字段的表。 查询的主要因素是一个名为Title
的字段。 应用程序应基于“标题”字段返回结果。 这两个记录的titles
是Sweet Street和Sweet Street 2 。
为了将结果作为JSON返回,我创建了一个类:
using System.Collections.Generic;
namespace IMES_Backend.Controllers
{
internal class Movie
{
public class BaseResponse
{
public List<Item> search { get; set; } = new List<Item>();
public bool response { get; set; }
}
public class Item
{
public string title { get; set; }
public string year { get; set; }
public string released { get; set; }
public string runTime { get; set; }
public string genre { get; set; }
public string director { get; set; }
public string writer { get; set; }
public string actors { get; set; }
public string language { get; set; }
public string country { get; set; }
public string awards { get; set; }
public string poster { get; set; }
public string imdbScore { get; set; }
public string production { get; set; }
public string dl480p { get; set; }
public string dl720p { get; set; }
public string dl1080p { get; set; }
public string subtitleLink { get; set; }
public string dubLink { get; set; }
public string description { get; set; }
public string state { get; set; }
}
}
}
然后,如前所述,选择该2行,并以JSON返回结果:
[Route("IMES/api/GET/search/t={movieTitle}")]
[HttpGet]
public IActionResult MovieSearch(string movieTitle)
{
string searchKeyword = movieTitle.Replace("%20", " ");
//Try to connect to the database
try
{
DB.dbConnection.Open();
MySqlCommand cmd = new MySqlCommand("select * from Movies where Title LIKE '%" + searchKeyword + "%'",DB.dbConnection);
DB.dataReader = cmd.ExecuteReader();
while (DB.dataReader.Read())
{
baseResponse.response = true;
//Create a list for storing the movie's data
baseResponse.search.Add(new Movie.Item
{
title = DB.dataReader.GetString(1),
year = DB.dataReader.GetString(2),
released = DB.dataReader.GetString(3),
runTime = DB.dataReader.GetString(4),
genre = DB.dataReader.GetString(5),
director = DB.dataReader.GetString(6),
writer = DB.dataReader.GetString(7),
actors = DB.dataReader.GetString(8),
language = DB.dataReader.GetString(9),
country = DB.dataReader.GetString(10),
awards = DB.dataReader.GetString(11),
poster = DB.dataReader.GetString(12),
imdbScore = DB.dataReader.GetString(13),
production = DB.dataReader.GetString(14),
dl480p = DB.dataReader.GetString(15),
dl720p = DB.dataReader.GetString(16),
dl1080p = DB.dataReader.GetString(17),
subtitleLink = DB.dataReader.GetString(18),
dubLink = DB.dataReader.GetString(19),
description = DB.dataReader.GetString(20),
state = DB.dataReader.GetString(21),
});
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);
}
}
catch(MySqlException ex) //MySql connection problem
{
return Ok(ex.Message);
}
finally
{
DB.dbConnection.Close();
DB.dataReader.Close();
}
baseResponse.response = false;
string error = JsonConvert.SerializeObject(baseResponse.response);
return Ok(error);
}
当我浏览.../IMES/api/GET/search/t=sweet
,我只获取第一行数据,第二行没有第二个列表,其中包含关键字sweet
。
我想在JSON的单独列表中获取两行的数据。 有人可以帮忙吗?
注意:我已经尝试在SQL Studio中进行查询,并且收到了两条记录。 所以我确定那2行!
您正在从while循环返回
while(){
retun Ok(response);
}
,因此在添加第一个项目后将其返回。 将其移到while循环之外
while(){
//do stuff
}
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.