2014-02-05 61 views
0

我是Windows手机开发的新手,所以通过引用实现了一些代码,但我无法获得所需的结果。在Windows Phone 8中JSON解析

我想解析从服务器收到的作为响应的JSON。 请在下面找到我的代码。

class JSONParsing 
    { 
     public Status response { get; set; } 
     public static void webClient1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (!String.IsNullOrEmpty(e.Result)) 
      { 
       try 
       { 

        JSONParsing root = JsonConvert.DeserializeObject<JSONParsing>(e.Result); 
        // root is null here 

        JObject obj = root.response.statusDetail; 
        // from here it goes to Catch block 
        foreach (KeyValuePair<string, JToken> pair in obj) 
        { 
         string key = pair.Key; 
         foreach (JObject detail in pair.Value as JArray) 
         { 
          string Code = detail["Code"].ToString(); 
          string Msg = detail["Msg"].ToString(); 
          string RegistrationID = detail["RegistrationID"].ToString(); 
          string Name = detail["Name"].ToString(); 
          string Phone = detail["Phone"].ToString(); 
          string email = detail["email"].ToString(); 
          string password = detail["password"].ToString(); 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("Cause of Exception is " + ex.Message); 

        // exception is-- "Object reference not set to an instance of an object." 
       } 
      } // if for empty 
     } 

    } 

    public class Status 
    { 
     public string Code { get; set; } 
     public string Msg { get; set; } 
     public object RegistrationID { get; set; } 
     public string Name { get; set; } 
     public string Phone { get; set; } 
     public string email { get; set; } 
     public string password { get; set; } 

     [JsonProperty("")] 
     public JObject statusDetail { get; set; } 
    } 
    public class RootObject 
    { 
     public List<Status> Status { get; set; } 
     public int success { get; set; } 
    } 

} 

请帮忙。

+0

我不太明白这个问题的阶级结构。 “*我无法得到想要的结果*”,不会添加太多信息。你期望的结果是什么,以及你从当前代码得到的结果是什么?请至少提供你得到的jason字符串样本。 – har07

回答

0

如果根为空类“JSONParsing”不具有相同的类结构的JSON

并且由于根是空,访问一个空(“root.response.statusDetail”)内属性将抛出异常

可以使用http://json2csharp.com/得到JSON