【发布时间】:2015-07-02 09:52:20
【问题描述】:
我有一个更新的 JSON 表,如下所示
{ "学生" : [ {"name": "Alex", "lastname": "Dean", "Properties" : [{"iq":120, "hair": "black", "glasses": “是”,“成绩”:[{“数学”:44,“点燃”:“88”}]},{“iq”:120,“头发”:“金发”,“眼镜”:“是”, “成绩”:[{“数学”:22,“点燃”:“81”}]},{“iq”:144,“头发”:“棕色”,“眼镜”:“是”,“成绩”: [{“数学”:44,“点燃”:“88”}]},{“iq”:120,“头发”:“红色”,“眼镜”:“是”,“成绩”:[{“数学” ": 44, "点亮": "88"}]}]}]}
我设法获取了不在子数组中的姓名和姓氏,但无法弄清楚如何获取子数组中的其他数据。在数组“Students”中,有一个名为“Properties”的子数组,在“Properties”中还有一个名为“Grades”的子数组。
如何获得“头发”和“数学”属性?
这是我获取姓名和姓氏的方法。
这是我的 WebService 类
public class WebService
{
public WebService ()
{
}
public async Task<Rootobject> GetStudentInfoAsync (string apiurl) {
var client = new HttpClient ();
var response = await client.GetStringAsync(string.Format(apiurl));
return JsonConvert.DeserializeObject<Rootobject>(response.ToString());
}
}
这是我想出的视图模型。为什么它不起作用。我没有得到值。
namespace MyApp
{
public class Grade
{
public string Math { get; set; }
public string Lit { get; set; }
}
public class Property
{
public int iq { get; set; }
public string hair { get; set; }
public string glasses { get; set; }
public Grade[] Grades { get; set; }
}
public class StudentInfo
{
public string name { get; set; }
public string lastname { get; set; }
public Property[] Properties { get; set; }
public string StudentName {
get{ return String.Format ("{0}", name); }
}
//Why isn't the following code work? I am not getting the values and I get errors.
public string HairColor {
get{ return String.Format ("{0}",Property.hair); }
}
public string MathGrade {
get{ return String.Format ("{0}", Property.Grade.Math); }
}
}
public class Rootobject
{
public StudentInfo[] students { get; set; }
}
}
这是我用学生姓名填充列表视图的方式
var sv = new WebService();
var es = await sv.GetStudentInfoAsync(apiurl);
Xamarin.Forms.Device.BeginInvokeOnMainThread( () => {
listView.ItemsSource = es.students;
});
var cell = new DataTemplate (typeof(TextCell));
listView.ItemTemplate = cell;
listView.ItemTemplate.SetBinding(TextCell.TextProperty, "StudentName");
【问题讨论】:
-
Properties数组有什么东西吗? -
没有。什么都得不到。
标签: c# json xamarin.forms