2012-02-03 127 views
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。

在此先感謝

回答

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

DataContractJsonSerializer區分大小寫。使用「runningfor」。