title: Converting JSON to C# Class Object
body: <p>Sorry, but I'm at a loss.  This process seems so simplistic to me, but no matter what I do currently I get a new object back with null and zero values.  No errors are thrown.  I have tried several different processes for converting JSON to a class object, but nothing has worked.  Below is the process I'd like to use.  Any help as to why this isn't working, would be greatly appreciated.</p>
  
  <p>Please note:  What I have to work with uses Hungarian notation.  I personally hate it.</p>
  
  <pre><code>//Incoming JSON string to convert:
  /*
  {"MapPolicySnapshot":{"strMapPolicyID":"189931809","lngLayerTypeID":0,"lngSnapShotID":0,"intZoomLevel":11,"strLayers":",Co    unty,HighRisk,Section,CLU,Policy,Draw","strDateChanged":"","strExtent":"-11405656.02395,5258291.144358,-11353411.315124,5282215.934208"}}
  */
  
  [Serializable]
  [DataContract(Name = "MapPolicySnapshot")]
  public class PolicySnapshot
  {
      [DataMember(Name = "strMapPolicyID")]
      public string strMapPolicyID { get; set; }
      [DataMember(Name = "lngLayerTypeID")]
      public long lngLayerTypeID { get; set; }
      [DataMember(Name = "lngSnapshotID")]
      public int lngSnapShotID { get; set; }  //Not a typo. Former developer.
      [DataMember(Name = "intZoomLevel")]
      public int intZoomLevel { get; set; }
      [DataMember(Name = "strLayers")]
      public string strLayers { get; set; }
      [DataMember(Name = "strDateChanged")]
      public string strDateChanged { get; set; }
      [DataMember(Name = "strExtent")]
      public string strExtent { get; set; }
  }
  
  public class AController
  {
      //All other code removed, and no, not the actual controller name
  
      private PolicySnapshot ConvertJSON(string snap)
      {
          // returns null and zeros
          //var snapShot = new JavaScriptSerializer().Deserialize&lt;PolicySnapshot&gt;(snap);
  
          var snapshot = DeserializeJSON&lt;PolicySnapshot&gt;(snap);
          return snapshot;
      }
  
      private T DeserializeJSON&lt;T&gt;(string json)
      {
          T obj = Activator.CreateInstance&lt;T&gt;();
          var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
          var serializer = new DataContractJsonSerializer(obj.GetType());
          obj = (T)serializer.ReadObject(ms);
          ms.Close();
          return obj;
      }
  }
  </code></pre>
  
  <p>When I create a new instance of the PolicySnapshot class with the values from the JSON string, then serialize, I get</p>
  
  <pre><code>{"strMapPolicyID":"189931809","lngLayerTypeID":0,"lngSnapShotID":0,"intZoomLevel":11,"strLayers":",County,HighRisk,Section,CLU,Policy,Draw","strDateChanged":"","strExtent":"-11405656.02395,5258291.144358,-11353411.315124,5282215.934208"}
  </code></pre>
  
  <p>which is the same data, minus the class name.</p>