JSON在Windows Phone 8中解析



我是Windows Phone开发的新手,因此通过参考已实现了一些代码,但我无法获得所需的结果。

我想解析作为服务器响应收到的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; }
    }
}

请帮助。

如果root为null,则类'JSONPARSING'与JSON

没有相同的类结构

并且由于root为null,因此访问null('root.Response.statusdetail')中的属性将抛出异常

您可以使用http://json2csharp.com/获取JSON的类结构

最新更新