在特定条件下在 gridview 和 listview 上显示 Json
Display Json on gridview and listview with specific condition
我在 gridview 中显示了 json。在 gridview 中显示标题,单击时将出现一个列表视图,该列表视图从 xfile 数组的 json 中获取数据。
JSON:
{
"error": false,
"total_data": 94,
"data_per_page": "20",
"total_page": 5,
"current_total": 20,
"tipe": "Video",
"data": [
{
"judul": "Kelas 01 Bab 02 Tematik Mengenal Bagian Tubuh",
"fname": "Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh.mp4",
"xfile": [
{
"url": "http://10.26.0.100:8080/video/SD_1/Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh.mp4",
"ukuran": 33295148,
"formated_size": "31.75M",
"res": "hd"
},
{
"url": "NOT EXIST",
"ukuran": 0,
"formated_size": "0.00B",
"res": "med"
},
{
"url": "http://10.26.0.100:8080/video/SD_1/converted/Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh_320x180.mp4",
"ukuran": 8278244,
"formated_size": "7.89M",
"res": "low"
}
]
},
{
"judul": "Kelas 01 Bab 04 Agama Islam Bersih Itu Sehat",
"fname": "Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat.mp4",
"xfile": [
{
"url": "http://10.26.0.100:8080/video/SD_1/Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat.mp4",
"ukuran": 56111407,
"formated_size": "53.51M",
"res": "hd"
},
{
"url": "NOT EXIST",
"ukuran": 0,
"formated_size": "0.00B",
"res": "med"
},
{
"url": "http://10.26.0.100:8080/video/SD_1/converted/Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat_320x180.mp4",
"ukuran": 11351698,
"formated_size": "10.83M",
"res": "low"
}
]
},
代码:
ObservableCollection<Video> datasource = new ObservableCollection<Video>();
ObservableCollection<XFile> data = new ObservableCollection<XFile>();
try
{
loading.Visibility = Visibility.Visible;
busyindicator.IsActive = true;
string urlPath1 = kelas.Link;
var httpClient1 = new HttpClient(new HttpClientHandler());
var values1 =
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("halaman", noHal.ToString()),
new KeyValuePair<string, string>("limit", limit.ToString())
};
var response1 = await httpClient1.PostAsync(urlPath1, new FormUrlEncodedContent(values1));
response1.EnsureSuccessStatusCode();
if (!response1.IsSuccessStatusCode)
{
busyindicator.IsActive = false;
RequestException();
}
string jsonText1 = await response1.Content.ReadAsStringAsync();
JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
JsonArray jsonData1 = jsonObject1["data"].GetArray();
foreach (JsonValue groupValue1 in jsonData1)
{
JsonObject groupObject2 = groupValue1.GetObject();
string title = groupObject2["judul"].GetString();
string cover = groupObject2.ContainsKey("cover") && groupObject2["cover"] != null ? groupObject2["cover"].GetString() : string.Empty;
string fname = groupObject2["fname"].GetString();
Video file1 = new Video();
file1.Judul = title;
file1.Cover = cover;
file1.FName = fname;
JsonArray pathJson = groupObject2["xfile"].GetArray();\
foreach (JsonValue groupValue2 in pathJson)
{
JsonObject groupObject3 = groupValue2.GetObject();
string url = groupObject3["url"].GetString();
string size = groupObject3["formated_size"].GetString();
string resolution = groupObject3["res"].GetString();
XFile file2 = new XFile();
file2.URL = url;
file2.Size = size;
file2.Resolution = resolution;
data.Add(file2);
}
datasource.Add(file1);
}
itemGridView.ItemsSource = datasource;
//...
private async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
AvaibleRes.IsOpen = true;
Video item = e.ClickedItem as Video;
DetailJudul.Text = item.Judul;
ResolutionList.ItemsSource = data;
}
private void ResolutionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ResolutionList.SelectedIndex != -1)
{
XFile res = e.AddedItems[0] as XFile;
DetailURL.Text = res.URL;
}
}
视频:
public class Video
{
public string Judul { get; set; }
public string FName { get; set; }
}
X文件:
public class XFile
{
public string URL { get; set; }
public string Size { get; set; }
public string Resolution { get; set; }
}
我有个问题,就是:点击gridview后,listview显示所有xfile数据。我要点击,例如:Class 01 Chapter 02 Thematic 知道了Body部分,那么listview中显示的数据只是xfile上那个数据而已。
如何处理?
首先尝试为您的 json 创建快速类型。
public class Xfile
{
public string url { get; set; }
public int ukuran { get; set; }
public string formated_size { get; set; }
public string res { get; set; }
}
public class video
{
public string judul { get; set; }
public string fname { get; set; }
public string cover { get; set; }
public ObservableCollection<Xfile> xfile { get; set; }
}
public class RootObject
{
public bool error { get; set; }
public int total_data { get; set; }
public string data_per_page { get; set; }
public int total_page { get; set; }
public int current_total { get; set; }
public string tipe { get; set; }
[JsonProperty("data")]
public ObservableCollection<video> videos { get; set; }
}
然后下面的代码使用 Newtonsoft.json
将您的 json 反序列化为您的快速类型
ObservableCollection<Video> datasource = new ObservableCollection<Video>();
ObservableCollection<XFile> data = new ObservableCollection<XFile>();
try
{
//Your code here
string jsonText1 = await response1.Content.ReadAsStringAsync();
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonText1);
itemGridView.ItemsSource = rootObject.videos.Select(x => new video { fname = x.fname, judul = x.judul, cover = x.cover }).ToList();
//Your code here
}
在你的ItemView_ItemClick
.
private async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
AvaibleRes.IsOpen = true;
video item = e.ClickedItem as video;
DetailJudul.Text = item.judul;
ResolutionList.ItemsSource = rootObject.videos.Where(x => x.judul == item.Judul)
.SelectMany(x => x.xfile).ToList();
}
注意: 全局创建 RootObject rootObject = new RootObject();
对象,以便您可以通过多个方法或事件访问它。
输出:
我在 gridview 中显示了 json。在 gridview 中显示标题,单击时将出现一个列表视图,该列表视图从 xfile 数组的 json 中获取数据。 JSON:
{
"error": false,
"total_data": 94,
"data_per_page": "20",
"total_page": 5,
"current_total": 20,
"tipe": "Video",
"data": [
{
"judul": "Kelas 01 Bab 02 Tematik Mengenal Bagian Tubuh",
"fname": "Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh.mp4",
"xfile": [
{
"url": "http://10.26.0.100:8080/video/SD_1/Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh.mp4",
"ukuran": 33295148,
"formated_size": "31.75M",
"res": "hd"
},
{
"url": "NOT EXIST",
"ukuran": 0,
"formated_size": "0.00B",
"res": "med"
},
{
"url": "http://10.26.0.100:8080/video/SD_1/converted/Kelas_01_Bab_02_Tematik_Mengenal_Bagian_Tubuh_320x180.mp4",
"ukuran": 8278244,
"formated_size": "7.89M",
"res": "low"
}
]
},
{
"judul": "Kelas 01 Bab 04 Agama Islam Bersih Itu Sehat",
"fname": "Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat.mp4",
"xfile": [
{
"url": "http://10.26.0.100:8080/video/SD_1/Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat.mp4",
"ukuran": 56111407,
"formated_size": "53.51M",
"res": "hd"
},
{
"url": "NOT EXIST",
"ukuran": 0,
"formated_size": "0.00B",
"res": "med"
},
{
"url": "http://10.26.0.100:8080/video/SD_1/converted/Kelas_01_Bab_04_Agama_Islam_Bersih_Itu_Sehat_320x180.mp4",
"ukuran": 11351698,
"formated_size": "10.83M",
"res": "low"
}
]
},
代码:
ObservableCollection<Video> datasource = new ObservableCollection<Video>();
ObservableCollection<XFile> data = new ObservableCollection<XFile>();
try
{
loading.Visibility = Visibility.Visible;
busyindicator.IsActive = true;
string urlPath1 = kelas.Link;
var httpClient1 = new HttpClient(new HttpClientHandler());
var values1 =
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("halaman", noHal.ToString()),
new KeyValuePair<string, string>("limit", limit.ToString())
};
var response1 = await httpClient1.PostAsync(urlPath1, new FormUrlEncodedContent(values1));
response1.EnsureSuccessStatusCode();
if (!response1.IsSuccessStatusCode)
{
busyindicator.IsActive = false;
RequestException();
}
string jsonText1 = await response1.Content.ReadAsStringAsync();
JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
JsonArray jsonData1 = jsonObject1["data"].GetArray();
foreach (JsonValue groupValue1 in jsonData1)
{
JsonObject groupObject2 = groupValue1.GetObject();
string title = groupObject2["judul"].GetString();
string cover = groupObject2.ContainsKey("cover") && groupObject2["cover"] != null ? groupObject2["cover"].GetString() : string.Empty;
string fname = groupObject2["fname"].GetString();
Video file1 = new Video();
file1.Judul = title;
file1.Cover = cover;
file1.FName = fname;
JsonArray pathJson = groupObject2["xfile"].GetArray();\
foreach (JsonValue groupValue2 in pathJson)
{
JsonObject groupObject3 = groupValue2.GetObject();
string url = groupObject3["url"].GetString();
string size = groupObject3["formated_size"].GetString();
string resolution = groupObject3["res"].GetString();
XFile file2 = new XFile();
file2.URL = url;
file2.Size = size;
file2.Resolution = resolution;
data.Add(file2);
}
datasource.Add(file1);
}
itemGridView.ItemsSource = datasource;
//...
private async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
AvaibleRes.IsOpen = true;
Video item = e.ClickedItem as Video;
DetailJudul.Text = item.Judul;
ResolutionList.ItemsSource = data;
}
private void ResolutionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ResolutionList.SelectedIndex != -1)
{
XFile res = e.AddedItems[0] as XFile;
DetailURL.Text = res.URL;
}
}
视频:
public class Video
{
public string Judul { get; set; }
public string FName { get; set; }
}
X文件:
public class XFile
{
public string URL { get; set; }
public string Size { get; set; }
public string Resolution { get; set; }
}
我有个问题,就是:点击gridview后,listview显示所有xfile数据。我要点击,例如:Class 01 Chapter 02 Thematic 知道了Body部分,那么listview中显示的数据只是xfile上那个数据而已。 如何处理?
首先尝试为您的 json 创建快速类型。
public class Xfile
{
public string url { get; set; }
public int ukuran { get; set; }
public string formated_size { get; set; }
public string res { get; set; }
}
public class video
{
public string judul { get; set; }
public string fname { get; set; }
public string cover { get; set; }
public ObservableCollection<Xfile> xfile { get; set; }
}
public class RootObject
{
public bool error { get; set; }
public int total_data { get; set; }
public string data_per_page { get; set; }
public int total_page { get; set; }
public int current_total { get; set; }
public string tipe { get; set; }
[JsonProperty("data")]
public ObservableCollection<video> videos { get; set; }
}
然后下面的代码使用 Newtonsoft.json
ObservableCollection<Video> datasource = new ObservableCollection<Video>();
ObservableCollection<XFile> data = new ObservableCollection<XFile>();
try
{
//Your code here
string jsonText1 = await response1.Content.ReadAsStringAsync();
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonText1);
itemGridView.ItemsSource = rootObject.videos.Select(x => new video { fname = x.fname, judul = x.judul, cover = x.cover }).ToList();
//Your code here
}
在你的ItemView_ItemClick
.
private async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
AvaibleRes.IsOpen = true;
video item = e.ClickedItem as video;
DetailJudul.Text = item.judul;
ResolutionList.ItemsSource = rootObject.videos.Where(x => x.judul == item.Judul)
.SelectMany(x => x.xfile).ToList();
}
注意: 全局创建 RootObject rootObject = new RootObject();
对象,以便您可以通过多个方法或事件访问它。
输出: