简体   繁体   English

Json中返回一些列表的问题:ASP.NET Core

[英]problem with returning some lists in Json : ASP.NET Core

I am creating an API which returns some results from a MySql database. 我正在创建一个API,该API从MySql数据库返回一些结果。 I have a table including 2 rows and some fields. 我有一个包含2行和一些字段的表。 The main factor for query is a field called Title . 查询的主要因素是一个名为Title的字段。 The application should return results based on the "title" field. 应用程序应基于“标题”字段返回结果。 The titles of these two records are Sweet Street and Sweet Street 2 . 这两个记录的titlesSweet StreetSweet Street 2

for returning the results as JSON, I have created a class: 为了将结果作为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; }

        }
    }
}

Then I select that 2 rows as previously mentioned and return the results in JSON: 然后,如前所述,选择该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);
    }

When I browse the .../IMES/api/GET/search/t=sweet , I just get the first rows data, there is no second list for the second row which includes the keyword sweet . 当我浏览.../IMES/api/GET/search/t=sweet ,我只获取第一行数据,第二行没有第二个列表,其中包含关键字sweet

I want to get the both rows' data in separated lists in JSON. 我想在JSON的单独列表中获取两行的数据。 Can anyone help? 有人可以帮忙吗?

Note: I have tried the query in SQL Studio and I have received two records. 注意:我已经尝试在SQL Studio中进行查询,并且收到了两条记录。 So I'm sure about that 2 rows! 所以我确定那2行!

You're returning from the while loop 您正在从while循环返回

while(){
    retun Ok(response);
}

, so it is returned after the first item is added. ,因此在添加第一个项目后将其返回。 Move it outside of the while loop 将其移到while循环之外

while(){
 //do stuff
}
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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