我是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)但我无法获得得分,测试日期和标记的值。请帮我解析这些细节。
提前致谢。
答案 0 :(得分: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();
}
}
}
希望它有所帮助!
答案 1 :(得分:0)
var result= JObject.Parse(response)["response"]["ScoreDetail"];
foreach (var item in result)
{
// Code to store the result
}