其他分享
首页 > 其他分享> > CodeGo.net>如何反序列化JSON字符串到域对象?

CodeGo.net>如何反序列化JSON字符串到域对象?

作者:互联网

我正在尝试将以下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字符串转换为某种中间格式)?

解决方法:

这是我将JSON反序列化回域对象的方式.

using (var reader = new MemoryStream(Encoding.Unicode.GetBytes("YourStringValue")))
{
     var ser = new DataContractJsonSerializer(typeof(Deal));
     return (Deal)ser.ReadObject(reader);
}

这是您要找的东西吗?

标签:windows-phone-7,json,c
来源: https://codeday.me/bug/20191201/2084378.html