samedi 26 juillet 2014

c# - désérialiser JSON dynamique nommé champ avec Json.NET - Stack Overflow


I get answer in JSON format from server like this (uid1, uid2...uidN - is dynamically named fields from server):


{
"get_message_state":
{
"uid1":"some text 1",
"uid2":"some text 2",
...
"uidN":"some text N"
},
"status":"OK_Operation_Completed"
}

When I try to describe a class to deserialize json response from server, i have a problem with get_message_state field. How to describe this field in class?


public class MessageStateResponse
{
[JsonProperty(PropertyName = "status", Order = 2)]
public string Status { get; set; }
[JsonProperty(PropertyName = "get_message_state", Order = 1)]
public Msg MessageState { get; set; } //??????????
}

public class Msg
{
[JsonProperty]
public Dictionary<string, string> Fields { get; set; } //??????????
}



You don't have to wrap dictionary to Msg object.


public class MessageStateResponse
{
[JsonProperty(PropertyName = "status", Order = 2)]
public string Status { get; set; }
[JsonProperty(PropertyName = "get_message_state", Order = 1)]
public Dictionary<string, string> Fields { get; set; }
}


I get answer in JSON format from server like this (uid1, uid2...uidN - is dynamically named fields from server):


{
"get_message_state":
{
"uid1":"some text 1",
"uid2":"some text 2",
...
"uidN":"some text N"
},
"status":"OK_Operation_Completed"
}

When I try to describe a class to deserialize json response from server, i have a problem with get_message_state field. How to describe this field in class?


public class MessageStateResponse
{
[JsonProperty(PropertyName = "status", Order = 2)]
public string Status { get; set; }
[JsonProperty(PropertyName = "get_message_state", Order = 1)]
public Msg MessageState { get; set; } //??????????
}

public class Msg
{
[JsonProperty]
public Dictionary<string, string> Fields { get; set; } //??????????
}


You don't have to wrap dictionary to Msg object.


public class MessageStateResponse
{
[JsonProperty(PropertyName = "status", Order = 2)]
public string Status { get; set; }
[JsonProperty(PropertyName = "get_message_state", Order = 1)]
public Dictionary<string, string> Fields { get; set; }
}

0 commentaires:

Enregistrer un commentaire