我正在使用json .net來serlize通用列表,但它帶回額外的數據在我的JSON respose時,我只想我的字段名稱和值。我曾嘗試使用下面的JSONConvert簡單語法,但即時通訊思考我將不得不基於從myList返回的值創建一個新的結果變量。Json從實體返回額外的值
public string GetListByUserId(int userId)
{
List<curoList> myList = _db.GetAllListsByUserId(userId);
var json = JsonConvert.SerializeObject(myList);
return json;
}
在數據的包括像EntitySetName時,我只是想等領域能像字段名:fieldValue方法,這使我對我的第二個問題與json.net如何將一個將數據轉換列表對象
編輯
我忘了,包括對u人的測試數據。
[{「$ id」:「1」,「id」:1,「firstName」:「David」,「lastName」:「Buckley」,「address1」:「」,空, 「縣」:空, 「郵政編碼」:空, 「EMAILADDRESS」:空, 「aboutme」:空, 「主動」:真 「的EntityKey」:{ 「$標識」: 「2」, 「EntitySetName」: 「persons」,「EntityContainerName」:「curoEntities」,「EntityKeyValues」:[{「Key」:「id」,「Type」:「System.Int32」,「Value」:「1」}]}}]
Eidt 2 這是展示我如何正在創建我的數據
public List<person> GetPersonByIdDal(int personId)
{
List<person> list = new List<person>();
try
{
list = (from myPersons in curoEntities.persons where myPersons.id == personId && myPersons.active==true select myPersons).ToList();
}
catch (Exception ex)
{
throw new EntityContextException("GetPersonById where active = true failed.", ex);
}
return list;
}
感謝您的投票,無論是誰;-) – rogue39nin
我們使用與json網絡的複雜對象的列表,它不會給我們任何額外的數據。我猜你看到這是因爲你的數據庫容器返回對象的方式。什麼是您的數據庫容器發出的實體類型。 – Prashant
您可以用['[JsonIgnore]']標記您不想要的屬性(http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm)。如果你想忽略所有的基類屬性,比如說,你可以做一些像https://stackoverflow.com/questions/30063259/jproperty-writeto-with-collections-in-a-jsonconverter。但要給出一個真正的答案,它將有助於查看問題的[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – dbc