无法正确反序列化 JSON

发布于 2024-12-11 01:47:52 字数 2982 浏览 1 评论 0原文

我有一个类

[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; }

    }

和一个 JSON 字符串:

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""}}";

我正在尝试使用此代码反序列化:

restaurant LR;

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

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

但它不起作用!奇怪的是,当我尝试反序列化它时,它没有显示错误(所以 json 很好,如果我故意搞乱 json,它会在运行时显示错误),但餐厅对象是空的。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-12-18 01:47:52

根据您的餐厅类别,我认为这是将由您拥有的代码反序列化的 json:

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""}";

不过我不能谈论日期。有时他们会抛出文化原因。

编辑:要使其序列化您的json,您可以创建一个外部类rest。就像,

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

那么您的反序列化代码也需要更改为:

rest LR;

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

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

大注释
如果您可以包含多个 [restaurant],例如

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

,那么您可能需要将 rest 类中的成员 restaurant 作为 array 制作为如下:

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

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文