Jonathan Hansen
Jonathan Hansen

Reputation: 443

Converting JSON to C# Class Object

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.

Please note: What I have to work with uses Hungarian notation. I personally hate it.

//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<PolicySnapshot>(snap);

        var snapshot = DeserializeJSON<PolicySnapshot>(snap);
        return snapshot;
    }

    private T DeserializeJSON<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        var serializer = new DataContractJsonSerializer(obj.GetType());
        obj = (T)serializer.ReadObject(ms);
        ms.Close();
        return obj;
    }
}

When I create a new instance of the PolicySnapshot class with the values from the JSON string, then serialize, I get

{"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"}

which is the same data, minus the class name.

Upvotes: 0

Views: 11268

Answers (2)

Jayme
Jayme

Reputation: 1946

You can try something like this:

    private object getClassFromJSon<T>(string JSon)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Deserialize<T>(JSon);
    }

and call it like this

var variableName = (MyClass)getClassFromJSon<MyClass>(JsonStringHere);

Upvotes: 0

Robert H
Robert H

Reputation: 11730

Personally I use RESTsharp as I find it makes serialization/deserialization pretty straight forward.

For instance I can deserialize an object with

orderInfo = JsonConvert.DeserializeObject<OrderStatusInfo>(responseString);

Taking your class and converting it to RESTsharp would look similar to yours, with some small alterations:

public class MapPolicySnapshot
{
    [JsonProperty("strMapPolicyID")]
    public long PolicyID { get; set; }

    [JsonProperty("lngLayerTypeID")]
    public long LayerTypeID { get; set; }

    [JsonProperty("lngSnapshotID")]
    public int SnapShotID { get; set; }

    [JsonProperty("intZoomLevel")]
    public int ZoomLevel { get; set; }

    [JsonProperty("strLayers")]
    public string Layers { get; set; }

    [JsonProperty("strDateChanged")]
    public string DateChanged { get; set; }

    [JsonProperty("strExtent")]
    public string Extent { get; set; }
}

and then doing something like:

MapPolicySnapshop snap = JsonConvert.DeserializeObject<MapPolicySnapshot>(responseString);

Upvotes: 1

Related Questions