Alex
Alex

Reputation: 11137

can't deserialize JSON correct

i have a class

[DataContract]
    public class restaurant
    {
        public restaurant() {}

        public restaurant(string Address, string Description, int Id, float Latitude, float Longitude, string Name,
            string OpeningHours, string Phone, string Sandwich, string Price, string UpdatedAt , string Website, float Score, int RatingCount, string ThumbnailUrl)
        {
            address = Address;
            description = Description;
            id = Id;
            latitude = Latitude;
            longitude = Longitude;
            name = Name;
            opening_hours = OpeningHours;
            phone = Phone;
            sandwich = Sandwich;
            price = Price;
            updated_at = UpdatedAt;
            website = Website;
            score = Score;
            rating_count = RatingCount;
            thumbnail_url = ThumbnailUrl;

        }


        [DataMember]
        public string address { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public float latitude { get; set; }
        [DataMember]
        public float longitude { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string opening_hours { get; set; }
        [DataMember]
        public string phone { get; set; }
        [DataMember]
        public string sandwich { get; set; }
        [DataMember]
        public string price { get; set; }
        [DataMember]
        public string updated_at { get; set; }
        [DataMember]
        public string website { get; set; }
        [DataMember]
        public float score { get; set; }
        [DataMember]
        public int rating_count { get; set; }
        [DataMember]
        public string thumbnail_url { get; set; }

    }

and a JSON String :

string json = @"{""restaurant"":{""address"":""blablabla"",""description"":""blablabla"",""id"":6,""latitude"":12.1,""longitude"":-12.3,
                                    ""name"":""blablabla"",""opening_hours"":""Mon-Fri: 12pm-2:30pm\r\n                  6:30pm-11pm \r\nSat: 12-3:30pm/6:30-11pm \r\nSun: 12-9pm"",""phone"":""123456"",""price"":""1"", ""sandwich"":""blablabla"",""updated_at"":""2011-10-10T21:40:17Z"",
                                    ""website"":""blablabla"",""score"":4.3,""ratings_count"":3,""thumbnail_url"":""http://website.com/1.jpg""}}";

I'm trying to deserialize using this code :

restaurant LR;

            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(restaurant));

                LR= (restaurant)serializer.ReadObject(ms);
            }

But it doesn't work! The weird thing is that it doesn't show an error when i try to deserialize it(so the json is good, if i intentionally screw the json up it shows an error in runtime), but the restaurant object is empty. What am i doing wrong?

Upvotes: 0

Views: 1205

Answers (1)

John Gathogo
John Gathogo

Reputation: 4655

Based on your restaurant class, I think this is the json that would be deserialized by the code you have:

string json = @"{""address"":""blablabla"",""description"":""blablabla"",""id"":6,""latitude"":12.1,""longitude"":-12.3,
                                ""name"":""blablabla"",""opening_hours"":""Mon-Fri: 12pm-2:30pm\r\n                  6:30pm-11pm \r\nSat: 12-3:30pm/6:30-11pm \r\nSun: 12-9pm"",""phone"":""123456"",""price"":""1"", ""sandwich"":""blablabla"",""updated_at"":""2011-10-10T21:40:17Z"",
                                ""website"":""blablabla"",""score"":4.3,""ratings_count"":3,""thumbnail_url"":""http://website.com/1.jpg""}";

I cant speak about the dates though. Sometimes they throw up cause of culture.

EDIT: To make it serialize your json, you could create an outer class rest. Like,

public class rest
{
    public restaurant restaurant { get; set; }
}

then your deserialization code would also need to change to:

rest LR;

using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
    DataContractJsonSerializer serializer =
                    new DataContractJsonSerializer(typeof(rest));

    LR = (rest)serializer.ReadObject(ms);
}

BIG NOTE If you can contain a number of [restaurant]s like

{"restaurant":[{""address"":"blah1", ...}, {""address"":"blah2", ...}]}

, then you may need to make the member restaurant inside rest class as an array as follows:

public class rest
{
    public restaurant[] restaurant { get; set; }
}

Upvotes: 1

Related Questions