简体   繁体   English

我可以将每个项目存储在 c# web api 的 foreach 循环内的 json 数组中吗?

[英]Can i able to store each item in a json array inside a foreach loop in c# web api?

I want to store each item to a json array which is placed inside a foreach loop.我想将每个项目存储到一个放在 foreach 循环内的 json 数组。

object array="";
foreach (DataRow drow1 in alltheatre.Rows)
        {
            string theatname = drow1["TheatreName"].ToString();
            string latitude = drow1["Latitude"].ToString();
            string longitude = drow1["Longitude"].ToString();
            string theatname = drow1["TheatreName"].ToString();
            string theatid = drow1["TheatreDetailsId"].ToString();
            string theataddre = drow1["TheatreAddress"].ToString();
            string cntctnu = drow1["ContactNum"].ToString();
            string theatimg = drow1["TheatreImage"].ToString();
            string descr = drow1["TheatreDesc"].ToString();
            string mouvieid = drow1["MovieMasterId"].ToString();
            if (mouvieid <100)
             {
                 array = new[]
                        {
                           new
                            {
                           Name = theatname,
                           Theatrdetailsid=theatid,
                           image=theatimg,
                           lat=latitude,
                           longi=longitude,
                           adress=theataddre,
                           contactnum=cntctnu,
                           desc=descr,
                            }
                          },
                        }
                      } 

Here.i have a foreach loop for getting details about a theatre,i want to store these datas to a json array,based on the condition.Right now i can only managed to store 1 data,ie the last one. Here.我有一个 foreach 循环来获取有关剧院的详细信息,我想根据条件将这些数据存储到 json 数组中。现在我只能设法存储 1 个数据,即最后一个。

The issue is that this code creates a new array with every iteration of the loop.问题是这段代码在每次循环迭代时都会创建一个新数组。 Try something like:尝试类似:

    using Newtonsoft.Json;

    string[] array = new string[]{};
    //data access and loop code
    int id = int.Parse(mouvieId);
    if (id < 100)
    {
        array[id] = JsonConvert.SerializeObject(new { Name = theatname /*rest of properties*/ });
    }

If the datagrid row object is of strongly typed then you can covert the rows collection to enumerable strongly typed.如果 datagrid 行对象是强类型的,那么您可以将行集合转换为可枚举的强类型。 Over the collection you can use Linq query to filter those records which matches the desired condition and in Select clause you can serialize the object using JsonCovert.SerializeObject method (from Newton.Json assembly).在集合上,您可以使用 Linq 查询来过滤那些符合所需条件的记录,在 Select 子句中,您可以使用 JsonCovert.SerializeObject 方法(来自 Newton.Json 程序集)序列化对象。

For ex :例如:
List theatres = alltheatre.Rows as List;列出剧院 = alltheatre.Rows 作为列表;
var serializedTheatres = theares.Where(t => t.MouvieId > 100).Select(t => JsonCovert.SerializeObject(t)).ToList(); var serializedTheatres = theares.Where(t => t.MouvieId > 100).Select(t => JsonCovert.SerializeObject(t)).ToList();

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

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