如何解析Windows Phone中8 JSON数据 [英] How to parse the Json data in windows phone 8

查看:110
本文介绍了如何解析Windows Phone中8 JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Windows Phone 8的开发。我工作的应用程序中,我需要解析JSON。 。所以我不能够得到的Windows Phone 8以下数据

I am new to windows phone 8 development. I am working on application in which I need parse the Json. so I am not able to get the following data in windows phone 8.

 {
   "response":{
      "errorFlag":0,
      "Score Detail":{
         "39":[
            {
               "test_date":"2013-06-28",
               "total_marks":"50",
               "score":"14"
            },
            {
               "test_date":"2013-08-08",
               "total_marks":"20",
               "score":"20"
            }
         ],
         "40":[
            {
               "test_date":"2013-08-08",
               "total_marks":"20",
               "score":"20"
            },
            {
               "test_date":"2013-08-08",
               "total_marks":"30",
               "score":"20"
            },
            {
               "test_date":"2013-08-08",
               "total_marks":"30",
               "score":"20"
            }
         ],
         "50":[
            {
               "test_date":"2013-08-08",
               "total_marks":"30",
               "score":"20"
            }
         ]
      }
   }
}

我试图解析通过以下方式

I am trying to parse the data in the following way

namespace testscore
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Mainpage_Loaded);
    }
 void Mainpage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient webClient1 = new WebClient();
        webClient1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient1_DownloadStringCompleted);
        webClient1.DownloadStringAsync(new Uri("some link"));
    }

public void webClient1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
        MessageBox.Show(e.Result.ToString());
        foreach (var res in rootObject.response.ScoreDetail)
        {
            string rs = res.Key;
            MessageBox.Show(rs.ToString());

            ......
        }                                          
    }

public class RootObject
    {
        public Response response { get; set; }
    }

    public class Response
    {
        public int errorFlag { get; set; }
        [JsonProperty("Score Detail")]
        public JObject ScoreDetail { get; set; }           
    }



在这里,我要得到键值(这里是39),但我我不能够得到的分数,testdate和标记的值。请帮我分析这些细节。

Here I am to getting key value (here it is 39) but I am not able to get the values of the score, testdate and marks. please help me in parsing these details.

先谢谢了。

推荐答案

我建议你建立你的JSON类:

I propose you to build classes of your json :

public class RootObject
{
    public Response response { get; set; }
}

public class Response
{
    public int errorFlag { get; set; }
    [JsonProperty("Score Detail")]
    public JObject ScoreDetail { get; set; }
}

您可以在DownloadStringCompleted事件中使用它们:

You can use them on the DownloadStringCompleted event :

public void webClient1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    RootObject root = JsonConvert.DeserializeObject<RootObject>(e.Result);
    JObject obj = root.response.ScoreDetail;
    foreach (KeyValuePair<string, JToken> pair in obj)
    {   
        string key = pair.Key; // here you got 39.
        foreach (JObject detail in pair.Value as JArray)
        {
            string date = detail["test_date"].ToString();
            string score = detail["score"].ToString();
            string total_marks = detail["total_marks"].ToString();
        }
    }
}



希望它帮助!

Hope it helps !

这篇关于如何解析Windows Phone中8 JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆