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

查看:35
本文介绍了如何在 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),但我无法获取分数、测试日期和分数的值.请帮我解析这些细节.

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

希望能帮到你!

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

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