简体   繁体   English

在.NET 3.5中使用WCF DataContractJsonSerializer

[英]Using the WCF DataContractJsonSerializer in .NET 3.5

I'm trying to use the geocoding code from here in my ASP.NET MVC 2 site. 我正在尝试使用ASP.NET MVC 2站点中的地理编码代码。 Unfortunately, some of that code, specifically the DataContractJsonSerializer usage, is only possible through .NET 4.0 . 不幸的是,某些代码,特别是DataContractJsonSerializer用法,只能通过.NET 4.0实现 As my hosting provider doesn't support .NET 4, I'm forced to implement that functionality in .NET 3.5. 由于我的托管服务提供商不支持.NET 4,我不得不在.NET 3.5中实现该功能。

How can I rework the code (which I have reposted below) to work in .NET 3.5? 我如何重新编写代码(我在下面重新发布)在.NET 3.5中工作?

The Google Maps Geocoding API can also return XML, if that's easier to serialize in 3.5... Google Maps Geocoding API也可以返回XML,如果在3.5中更容易序列化...


Below is the code I'm trying to convert from .NET 4 to .NET 3.5: 下面是我试图从.NET 4转换为.NET 3.5的代码:

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Web;

.
.
.

private static GeoResponse CallGeoWS(string address)
{
        string url = string.Format(
                "http://maps.google.com/maps/api/geocode/json?address={0}&region=dk&sensor=false",
                HttpUtility.UrlEncode(address)
                );
        var request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
        var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
        return res;
}

[DataContract]
class GeoResponse
{
        [DataMember(Name="status")]
        public string Status { get; set; }
        [DataMember(Name="results")]
        public CResult[] Results { get; set; }

        [DataContract]
        public class CResult
        {
                [DataMember(Name="geometry")]
                public CGeometry Geometry { get; set; }

                [DataContract]
                public class CGeometry
                {
                        [DataMember(Name="location")]
                        public CLocation Location { get; set; }

                        [DataContract]
                        public class CLocation
                        {
                                [DataMember(Name="lat")]
                                public double Lat { get; set; }
                                [DataMember(Name = "lng")]
                                public double Lng { get; set; }
                        }
                }
        }
}

What is the specific problem that you're hitting? 你遇到的具体问题是什么?

Without more details it's difficult to diagnose the exact problem, but DataContractJsonSerializer is available in .NET 3.5 - you'll need to manually add a reference to System.ServiceModel.Web.dll . 如果没有更多的细节很难诊断确切的问题,但DataContractJsonSerializer .NET 3.5可用-你需要一个参考手动添加到System.ServiceModel.Web.dll。

(Note that the MSDN documentation misleadingly states that DataContractJsonSerializer can be found in System.Runtime.Serialization.dll . While this is true for .NET 4, the .NET 3.5 version of DataContractJsonSerializer actually lives in System.ServiceModel.Web.dll .) (请注意,MSDN文档误导性地指出可以在System.Runtime.Serialization.dll中找到DataContractJsonSerializer 。虽然这适用于.NET 4,但.NET 3.5版本的DataContractJsonSerializer实际上存在于System.ServiceModel.Web.dll中 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM