2015-10-13 81 views
0

我有一個類對象:如何保留屬性名稱?

public class ChildDetails 
{ 
    [JsonProperty("idConsecutivo")] 
    public int SequentialId { get; set; } 
    [JsonProperty("idSemilla")] 
    public int ParentId { get; set; } 
    [JsonProperty("idParentSemilla")] 
    public int ChildId { get; set; } 
    [JsonProperty("NombreArchivo")] 
    public string FileName { get; set; }   
} 

當執行這一行:

var childItems = JsonConvert.DeserializeObject<List<ChildDetails>>(resultContent); 

childItems對象屬性的名稱是:SequentialId,的ParentId,childID的名和文件名

但是當執行此line:

var otherChildItems = JsonConvert.SerializeObject(childItems); 

otherChildItems對象屬性的名稱是:idConsecutivo,idSemilla,idParentSemilla,NombreArchivo

如何保持的屬性名稱SequentialId,的ParentId,childID的名和文件名當對象被序列化?

感謝

+0

如果你需要使用一個組名稱序列化和使用不同的反序列化一組名稱,那麼你可能想看看這個答案:http://stackoverflow.com/questions/22993576/jsonproperty-use-different-name-for-deserialization-but-use-original-name-for – dontangg

+0

謝謝@ dontangg – ericardezp

回答

0

有規律的屬性名稱根本不添加JsonProperty屬性,它們允許你指定替代名稱:

public class ChildDetails 
{ 
    public int SequentialId { get; set; } 
    ... 
} 
+0

謝謝Alexei Levenkov – ericardezp

相關問題