3

我是 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),但我无法获取分数、测试日期和分数的值。请帮助我解析这些细节。

提前致谢。

4

2 回答 2

6

我建议你建立你的 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();
        }
    }
}

希望能帮助到你 !

于 2013-08-08T07:45:43.780 回答
0
var result= JObject.Parse(response)["response"]["ScoreDetail"];
                                    foreach (var item in result)
                                    {

// Code to store the result
}
于 2013-11-13T11:03:47.027 回答