1

I have to return data in json format but it says cannot implicitly convert type string to system.collection.generic.list(object).

 [HttpGet]
    public List<object> GetAllCompanies2()
    {
        List<object> myCo = DefCompany.AllDefCompany2;
         // return myCo----------> works

        string json = JsonConvert.SerializeObject(myCo);
        return  json; ------- > not works conversion error & i need this
    }

I tried tostring() and many other ways but nothing worked. How can I convert string to object?
Here is my function code AllDefCompany2

   public static List<object> AllDefCompany2
    {
        get
        {
            using (var db = new RPDBEntities())
            {

                return db.DefCompanies.Select(b => new 
                {
                    Id = b.Id,

                    CurrentCurrencyCode = b.CurrentCurrencyCode,
                    ShortName = b.ShortName,
                    FullName = b.FullName,
                    ContactPerson = b.ContactPerson,
                    Address1 = b.Address1,
                    CompanyCity = b.CompanyCity,
                    CompanyState = b.CompanyState,
                    CompanyCountry = b.CompanyCountry,
                    ZipPostCode = b.ZipPostCode,
                    TelArea = b.TelArea

                }).ToList<object>();


            }

        }

    }
1
  • JsonConvert.SerializeObject(myCo) doesn't return the data in a json format, rather as a string that can be later parsed Commented May 27, 2015 at 6:56

5 Answers 5

2

this code helps me to solve both api and kendo problem

[HttpGet]
public List<object> GetAllCompanies2()
{
    List<object> myCo = DefCompany.AllDefCompany2;         

    object json = JsonConvert.SerializeObject(myCo);
    return  json;
}
Sign up to request clarification or add additional context in comments.

Comments

1

here is configuration settings

 using System.Data.Entity;

  namespace RpManticSolAPI
  {
public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);          
    }
}
 }

Comments

0
return JsonConvert.SerializeObject(myCo);

4 Comments

Just code is not enough, explain what you are doing here
Assuming the object myCo returns is the List. The error is because you are returning a STRING variable. (return json) whereas the method is expecting a return type of List<object>
the return value of JsonConvert.SerializeObject(myCo) is still a string, it won't make a difference if you don't store it in a variable first
i have to call api and populate grid , grid sucessfully populated but api during testing says: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
0

You can convert string to object, but what you are trying to do in the code shown is convert it to a List<object>. You can try calling AsEnumerable or ToList on the json variable.

But if what you want to do is return a string, why not change the return type of the method?

Comments

0
[HttpGet]
public string GetAllCompanies2()
{
    List<object> myCo = DefCompany.AllDefCompany2;
     // return myCo----------> works

    string json = JsonConvert.SerializeObject(myCo);
    return  json; ------- > not works conversion error & i need this
}

Try this. Need to change function return type based on whether you return list of objects or string (in this case json)

1 Comment

i have to populate kendo grid even in this way my kendo grid does not populate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.