簡體   English   中英

如何在Windows Phone 8中解析Json數據

[英]How to parse the Json data in windows phone 8

我是Windows Phone 8開發的新手。 我正在處理我需要解析Json的應用程序。 所以我無法在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"
            }
         ]
      }
   }
}

我試圖以下面的方式解析數據

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),但我無法獲得得分,測試日期和標記的值。 請幫我解析這些細節。

提前致謝。

我建議你構建你的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事件中使用它們:

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

希望能幫助到你 !

var result= JObject.Parse(response)["response"]["ScoreDetail"];
                                    foreach (var item in result)
                                    {

// Code to store the result
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM