【发布时间】:2015-05-27 06:47:06
【问题描述】:
我必须以 json 格式返回数据,但它说不能将类型字符串隐式转换为 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
}
我尝试了 tostring() 和许多其他方法,但没有任何效果。如何将字符串转换为对象?
这是我的功能代码 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>();
}
}
}
【问题讨论】:
-
JsonConvert.SerializeObject(myCo)不会以json格式返回数据,而是以string的形式返回,以便稍后解析
标签: c# asp.net json vb.net asp.net-web-api