Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;

        }


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"}]}
Posted
Updated 22-Jun-17 10:37am
Comments
F-ES Sitecore 23-Jun-17 5:03am    
Google "deserialise json c#" - there are loads of examples on how to do this.

1 solution

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;
}

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900