无法正确反序列化 JSON
我有一个类
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的餐厅类别,我认为这是将由您拥有的代码反序列化的 json:
不过我不能谈论日期。有时他们会抛出文化原因。
编辑:要使其序列化您的json,您可以创建一个外部类rest。就像,
那么您的反序列化代码也需要更改为:
大注释
如果您可以包含多个 [restaurant],例如
,那么您可能需要将 rest 类中的成员 restaurant 作为 array 制作为如下:
Based on your restaurant class, I think this is the json that would be deserialized by the code you have:
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,
then your deserialization code would also need to change to:
BIG NOTE
If you can contain a number of [restaurant]s like
, then you may need to make the member restaurant inside rest class as an array as follows: