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 に答える