我有一个班级
[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它在运行时显示错误),但餐厅对象是空的。我做错了什么?
答案 0 :(得分:1)
根据您的餐厅类,我认为这是将通过您的代码反序列化的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,您可以创建外部类休息。像,
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);
}
BIG NOTE 如果你可以包含一些[餐厅]像
{"restaurant":[{""address"":"blah1", ...}, {""address"":"blah2", ...}]}
,那么您可能需要将 rest 类中的成员 restaurant 作为数组,如下所示:
public class rest
{
public restaurant[] restaurant { get; set; }
}