我正在尝试将以下json转换为域对象。
JSON:
[{"name":"McDonalds","deals":[{"desc":"Onion Rings free with Double Cheese burgers"}],"geometry":{"lat":13.073412,"lng":80.221393}},{"name":"JusSportz","deals":[{"desc":"20% off on Wilson Rackets"}],"geometry":{"lat":13.073231,"lng":80.221181}}]
型号:
[DataContract]
public class Deal
{
[DataMember(Name="name")]
public string Store { get; set; }
[DataMember(Name="deals")]
public Offer[] Offers {get; set;}
[DataMember(Name="geometry")]
public GeoCoordinate Location { get; set; }
}
[DataContract]
public class Offer
{
[DataMember]
public string deal;
}
我尝试了System.Runtime.Serialization.Json命名空间中的 DataContractJsonSerializer ,显然没有办法将字符串转换为对象。有没有其他图书馆允许我完成这项工作?或者有没有办法使用这个库完成此操作(通过将Json字符串转换为某种中间格式。)?
答案 0 :(得分:2)
以下是我如何将JSON反序列化回我的域对象。
using (var reader = new MemoryStream(Encoding.Unicode.GetBytes("YourStringValue")))
{
var ser = new DataContractJsonSerializer(typeof(Deal));
return (Deal)ser.ReadObject(reader);
}
这是你在找什么?
答案 1 :(得分:1)
尝试更改一下模型:
[DataContract]
public class Deal
{
[DataMember(Name="name")]
public string Store { get; set; }
[DataMember(Name="deals")]
public Offer[] Offers {get; set;}
[DataMember(Name="geometry")]
public GeoCoordinate Location { get; set; }
}
[DataContract]
public class Offer
{
[DataMember(Name="desc")]
public string deal { get; set; }
}