在ASP MVC API中将数组转换为对象 [英] Convert array to object in ASP MVC API

查看:91
本文介绍了在ASP MVC API中将数组转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用stringbuilder将对象转换为我的api中的数组,但是我没有得到它。

i know数组是[],对象是{}。需要帮助。请帮我查询和stringbuilder.thank你



我尝试过:



这是api控制器



i want to use stringbuilder to convert the object to array in my api but i don't get it.
i know array is [] and object is {}.you help is needed.please help me with the query and the stringbuilder.thank you

What I have tried:

this is the api controller

public ApiResult<List<Event>> GetUpcomingEvents()
        {
            EventMasterDb repo = new EventMasterDb();
            var result = new ApiResult<List<Event>>();
            try
            {
                StringBuilder  query = new StringBuilder();

                var list = repo.Fetch<Event>(query.ToString());
                result.Successfull = 1;
                result.Model = list;
            }
            catch (Exception ex)
            {
                result.Successfull = 0;
                result.InternalError = ex.Message;
                result.Error = "Error from server";
            }
            finally
            {
                repo.Dispose();
            }

            return result;

        }





这是来自api的结果,它的花括号,对象类型





this is the results from api,its in curly braces,that object type

{"EventId":2,"EventName":"Vodafone Ghana Videos Awards","Description":"Awards for music videos in Ghana","MerchantId":1,"VenueId":2,"VenueName":"Accra International Conference Center","Latitude":null,"Longitude":null,"Thumbnail":null,"CoverImage":null,"VideoUrl":null,"Url":null,"AddedDate":"2017-06-08T00:00:00","PublishedDate":"2017-06-10T00:00:00","EventDate":"2017-06-15T00:00:00"}]}

推荐答案

从技术上讲,你的需求不明确且含糊不清。你为什么要那样做?如何将对象转换为数组?



一种简单的方法是,接受对象,创建一个数组,将该对象添加到数组,并序列化数组。 :D我的意思是,这是一种非常超现实的场景,你需要这样做。您可能有多种方法可以执行此操作,例如,服务器或客户端可能正在寻找阵列响应。但是,这不是一个好方法,您需要重新考虑您的API协议,动词和资源共享。



您的代码显示了这样做的一个很好的示例,我可以看到您已经在 repo <中有一个集合列表/ code> object。

Technically your need is unclear and very much ambiguous. Why do you need to do that? How can you convert, an object to an array?

One simple way would be, to accept the object, create an array, add that object to the array, and serialize the array. :D I mean, this is a very surreal sort of scenario, in which you would be needed to do this. There can be several ways where you might want to do this, such as, server or client might be looking for an array response. But nonetheless, this is not a good approach, you need to rethink your API protocol, verb and resource sharing.

Your code is showing a good example of doing this, I can see that you already have a list of your collection there in the repo object.
public List<Event> GetUpcomingEvents()
{
   EventMasterDb repo = new EventMasterDb();
   StringBuilder  query = new StringBuilder();
   // Missed the query? 
   var list = repo.Fetch<List<Event>>(query.ToString()); 
   // Fetch List<Event> instead of Event

   // You already have a list, return it!
   return list;
}



还有一件事,API不要求你使用MVC包装器,比如ActionResults等,你只需要返回自己的自定义类型,或容器( List 等)。 ASP.NET Web API将自动将它们映射到您需要的JSON或XML格式。


One more thing, the API do not require you to use MVC wrappers, such as ActionResults etc, you just need to return your own custom types, or containers (List etc). ASP.NET Web API will automatically map them to what you need in the JSON or XML format.


这篇关于在ASP MVC API中将数组转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆