将JSON转换为C#类对象

时间:2014-03-18 16:33:53

标签: c# json javascriptserializer

抱歉,我不知所措。这个过程对我来说似乎过于简单,但无论我现在做什么,我都会得到一个带有null和零值的新对象。没有错误被抛出。我已经尝试了几种不同的过程来将JSON转换为类对象,但没有任何工作。以下是我想要使用的流程。任何帮助,为什么这不起作用,将不胜感激。

请注意:我必须使用匈牙利表示法。我个人讨厌它。

//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;
    }
}

当我使用JSON字符串中的值创建PolicySnapshot类的新实例时,然后序列化,我得

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

这是相同的数据,减去班级名称。

2 个答案:

答案 0 :(得分:1)

我个人使用RESTsharp,因为我发现序列化/反序列化很简单。

例如,我可以使用

反序列化对象

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

将您的课程转换为RESTsharp看起来与您的相似,但有一些小改动:

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; }
}

然后做类似的事情:

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

答案 1 :(得分:0)

您可以尝试这样的事情:

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

并像这样称呼它

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