Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6747725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:28:42+00:00 2026-05-26T12:28:42+00:00

i have a class [DataContract] public class restaurant { public restaurant() {} public restaurant(string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:28:42+00:00Added an answer on May 26, 2026 at 12:28 pm

    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; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes as below: [DataContract] public class Address { [DataMember] public string
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
I have the following DTO definition:- [DataContract] public class AddProductDTO { [DataMember] public string
I have a class in WCF: [DataContract] public class Usuario { [DataMember] public int
I have a class [DataContract] public class Car { public bool pbIsHatchBack; string prSt
I have de folowing classes : [DataContract] public class MyProject { [DataMember(Name = Branches)]
I have a simple data contract: [DataContract] public class MyData { [DataMember] public string
I have a class Team that holds a generic list: [DataContract(Name = TeamDTO, IsReference
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
Hi I have class to sen via ria service. class look like [DataContract] public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.