JSON WCF反序列化DataContracts

时间:2014-03-15 05:12:17

标签: c# json web-services wcf datacontract

我有一个DataContract,我试图从WCF服务收到的一些JSON反序列化。我可以让根DeviceIDTimeStamp反序列化而不会出现问题,但是,我无法获得GeoLocationsVideosVideoFrames中的任何内容反序列化。

我已经看了很多,从我能找到的这应该有效。我已经调整了一些DataContact属性,试图得到这个,但仍然没有运气。

我将此JSON发送到WCF Web服务。

{
    "DeviceId": "74a5f7aa9d4dfd0d",
    "TimeStamp": 1394756724,
    "GeoLocations": [
        {
            "TimeStamp": 1394756724,
            "Latitude": 948.348275,
            "Longitude": 381.3252
        },
        {
            "TimeStamp": 1394756726,
            "Latitude": 98.348275,
            "Longitude": 31.3252
        }
    ],
    "Videos": [
        {
            "VideoFrames": [
                {
                    "TimeStamp": 1234567789,
                    "Base64EncodedFrame": "dajflahfaskfasld"
                },
                {
                    "TimeStamp": 1234567789,
                    "Base64EncodedFrame": "dajflahfaskfasld"
                }
            ]
        }
    ]
}

Web服务界面看起来像这样。

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "json")]
String JSONData();

Web服务尝试将对象反序列化为此模型。

[DataContract(Namespace="")]
public class VideoFrame
{
    [Key]
    public int VideoFrameId { get; set; }

    [DataMember(Order = 0, IsRequired = false)]
    public int TimeStamp { get; set; }

    [DataMember(Order = 1, IsRequired = false)]
    public string Base64EncodedFrame { get; set; }
}

[DataContract(Namespace = "")]
public class Video
{
    [Key]
    public int VideoId { get; set; }

    [DataMember(Order = 0, IsRequired = false)]
    public virtual List<VideoFrame> VideoFrames { get; set; }
}

[DataContract(Namespace="")]
public class GeoLocation
{
    [Key]
    public int GeoLocationId { get; set; }

    [DataMember(Order = 0, IsRequired = true)]
    public int TimeStamp { get; set; }

    [DataMember(Order = 1, IsRequired = true)]
    public double Latitude { get; set; }

    [DataMember(Order = 2, IsRequired = true)]
    public double Longitude { get; set; }
}

[DataContract(Name="root", Namespace="")]
public class DeviceUpdate
{
    [Key, Column(Order = 0)]
    [DataMember (Order=0, IsRequired=true)]
    public string DeviceId { get; set; }

    [Key, Column(Order = 1)]
    [DataMember(Order=1, IsRequired=true)]
    public int TimeStamp { get; set; }

    [DataMember(Order = 2, IsRequired = true)]
    public virtual List<GeoLocation> GeoLocations { get; set; }

    [DataMember(Order = 3, IsRequired = true)]
    public virtual List<Video> Videos { get; set; }
}

网络服务代码是:

DeviceUpdate dataObject = OperationContext.Current.RequestContext.RequestMessage.GetBody<DeviceUpdate>();

Webconfig:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="BugsBunny.RestService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="BugsBunny.IRestService" behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.web>
    <customErrors mode="Off"/>
  </system.web>
</configuration>

这也可能有所帮助,我在恢复JSON之后提取了它试图处理的实际XML:

<root type="object">
  <DeviceId type="string">74a5f7aa9d4dfd0d</DeviceId>
  <TimeStamp type="number">1394756724</TimeStamp>
  <GeoLocations type="array">
    <item type="object">
      <TimeStamp type="number">1394756724</TimeStamp>
      <Latitude type="number">948.348275</Latitude>
      <Longitude type="number">381.3252</Longitude>
    </item>
    <item type="object">
      <TimeStamp type="number">1394756726</TimeStamp>
      <Latitude type="number">98.348275</Latitude>
      <Longitude type="number">31.3252</Longitude>
    </item>
  </GeoLocations>
  <Videos type="array">
    <item type="object">
      <VideoFrames type="array">
        <item type="object">
          <TimeStamp type="number">1234567789</TimeStamp>
          <Base64EncodedFrame type="string">dajflahfaskfasld</Base64EncodedFrame>
        </item>
        <item type="object">
          <TimeStamp type="number">1234567789</TimeStamp>
          <Base64EncodedFrame type="string">dajflahfaskfasld</Base64EncodedFrame>
        </item>
      </VideoFrames>
    </item>
  </Videos>
</root>

2 个答案:

答案 0 :(得分:2)

好的,我找到了一种方法来完成这项工作。

我已将网络服务实施更改为:

DeviceUpdate dataObject = OperationContext.Current.RequestContext.RequestMessage.GetBody<DeviceUpdate>(new DataContractJsonSerializer(typeof(DeviceUpdate)));

并补充道:

RequestFormat = WebMessageFormat.Json

到服务接口属性。

这允许所有对象填充数据联系人。

感谢大家的帮助。

答案 1 :(得分:1)

在这里你不会得到地理位置,因为BodyStyle bare不包含名称空间,而包装就是这样,所以你也可以获得其他属性。

试试这个

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/json")]
String JSONData();