Craig Benson
Craig Benson

Reputation: 105

Deserializing JSON that contains a variable that begins with "@"

I am working on a Windows 8 app wherein I need to deserialize a JSON feed that contains variables which begin with an '@' sign. I've defined classes that have members with the same names as the variables in the JSON, then call DataContractJsonSerializer to deserialize the JSON into C# classes. This all works fine and dandy except for the variable names that begin with '@'. Like this:

public class HotelDetails
{
    public string hotelId;
    public string name;
    public string address1;
...

}

The JSON looks like this:

{"@order":"0",
   "hotelId":268026,
   "name":"Monte Cristo",
   "address1":"600 Presidio Avenue",
...

Since I can't define a C# variable that begins with '@' how do I deserialize the "@order" variable?

Upvotes: 2

Views: 246

Answers (1)

luksan
luksan

Reputation: 7757

Try this?

[DataContract]
public class HotelDetails
{
    [DataMember(Name="@order")]
    public string order;

    [DataMember(Name="hotelId")]    
    public string hotelId;

    [DataMember(Name="name")]  
    public string name;

    [DataMember(Name="address1")]  
    public string address1;
}

Don't know if that works though... haven't tested it.

Upvotes: 3

Related Questions