2016-12-18 96 views
0

有沒有一種方法可以讓我改變列/屬性名稱我從通過Web API JSON響應返回?有沒有辦法來改變屬性名稱的JSON數據

樣品JSON響應:

[ 
{ 
    "ActualPropName1": "Value1", //change ActualPropName1 to something else 
    "ActualPropName2": "Value2" 
} 
] 

我試圖在模型中使用的數據標註,但是,這似乎並沒有達到我想要的

[DisplayName("Generic Name")] 
public string ActualPropName1{ get; set; } 
+0

您可以使用[Json.NET](http://www.newtonsoft.com/json)和一個適用於您的財產的[JsonProperty(PropertyName =「FooBar」)]' –

+0

應用上面的建議,但JSON仍然返回實際的屬性名稱。 – hello

+1

這是用於序列化還是反序列化?如果序列化,那麼爲什麼不直接返回一個匿名對象,或者在序列化之前將其映射到另一個具有替代名稱的視圖模型 –

回答

-1

您可以使用JSON.NET的JsonProperty

[JsonProperty(PropertyName="Your_Name")] 
public string ActualPropName1{ get; set; } 
2

您可以簡單地擁有一個新的屬性,其名稱只需返回其他數據r財產。然後隱藏/忽略其他:

[JsonIgnore] 
public string ActualPropName1 { get; set; } 

public string GenericName 
{ 
    get 
    { 
     return ActualPropName1; 
    } 
} 

雖然從您的示例中這不會爲'通用名稱'的屬性名稱工作。

相關問題