0

我的第一次尝试和 JSON 反序列化,我被困住了,只是想知道您是否可以提供帮助?

我有以下 JSON

{
    "summary":{
        "pricing":{
            "net":988,
            "tax":13,
            "gross":729
        },
        "status":{
            "runningfor":29881175,
            "stoppedfor":88805,
            "idlefor":1298331744
        }
    }
}

这是我的 C# 代码

private void MakeRequest()
{
    HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest; 
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Credentials = new NetworkCredential(Username, Password);
    request.Headers.Add(string.Format("App-Key: {0}", ApiKey));

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string resutls = reader.ReadToEnd();
        Response.Write(resutls);
        Status status = JSONHelper.JsonDeserialize<Status>(resutls);
        Response.Write(status.RunningFor);
    }
}

public class JSONHelper
{
    /// <summary>
    /// JSON Deserialization
    /// </summary>
    public static T JsonDeserialize<T>(string jsonString)
    {
        T obj = Activator.CreateInstance<T>();
        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
        obj = (T)serializer.ReadObject(ms);
        ms.Close();
        return obj;
    }
}


[DataContractAttribute(Name = "status")]
public class Status
{
    [DataMember(Name = "runningfor")]
    public int RunningFor{ get; set; }
    [DataMember(Name = "stoppedfor")]
    public int StoppedFor{ get; set; }
    [DataMember(Name = "idlefor")]
    public int IdleFor{ get; set; }
}

而且我只对状态结果感兴趣,根本不关心。我做错了什么,因为它只为 RunningFor 返回 0。

提前致谢

4

2 回答 2

1

您需要反序列化为一个结构,该结构映射到您尝试反序列化的整个 JSON,而不仅仅是您想要的部分。在您的情况下,下面的代码显示了一种方法。

public class StackOverflow_9135439
{
    const string JSON = @"{
    ""summary"":{
        ""pricing"":{
            ""net"":988,
            ""tax"":13,
            ""gross"":729
        },
        ""status"":{
            ""runningfor"":29881175,
            ""stoppedfor"":88805,
            ""idlefor"":1298331744
        }
    }
}";
    [DataContractAttribute(Name = "status")]
    public class Status
    {
        [DataMember(Name = "runningfor")]
        public int RunningFor { get; set; }
        [DataMember(Name = "stoppedfor")]
        public int StoppedFor { get; set; }
        [DataMember(Name = "idlefor")]
        public int IdleFor { get; set; }
    }

    [DataContract]
    public class Summary
    {
        [DataMember(Name = "status")]
        public Status Status { get; set; }

        // add "pricing" later if you need
    }

    [DataContract]
    public class Response
    {
        [DataMember(Name = "summary")]
        public Summary Summary { get; set; }
    }

    public class JSONHelper
    {
        /// <summary>
        /// JSON Deserialization
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            T obj = Activator.CreateInstance<T>();
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            obj = (T)serializer.ReadObject(ms);
            ms.Close();
            return obj;
        }
    }

    public static void Test()
    {
        Response resp = JSONHelper.JsonDeserialize<Response>(JSON);
        Console.WriteLine(resp.Summary.Status.RunningFor);
    }
}
于 2012-02-03T21:34:14.783 回答
0

DataContractJsonSerializer 区分大小写。使用“跑步”。

于 2012-02-03T21:34:18.477 回答