将 json 数据加载到 wpf 数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20947763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
load json data to wpf datagrid
提问by autopilot
I want to load json data to my wpf datagrid. I have followed this tutorial: http://wpf-4-0.blogspot.com/2012/12/how-to-bind-json-data-to-datagrid-in.html
我想将 json 数据加载到我的 wpf 数据网格。我遵循了本教程:http: //wpf-4-0.blogspot.com/2012/12/how-to-bind-json-data-to-datagrid-in.html
But something is going wrong and I am not sure where is the problem.
但是出了点问题,我不确定问题出在哪里。
my xaml code:
我的 xaml 代码:
<Button Click="btnLoad_Click" Content="Load" Width="50" Grid.Row="0"></Button>
<DataGrid Grid.Row="1" x:Name="albuminfo" AutoGenerateColumns="True"></DataGrid>
C# code:
C#代码:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
WebRequest req = WebRequest.Create("http://coderomusic.com/beta/SecuredAdministrator/mobile/getInfo.php?_mod=album");
req.ContentType = "application/json";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader re = new StreamReader(stream);
string json = re.ReadToEnd();
json = "{\"AlbumDetail\":" + json + "}";
Wrapper w = (Wrapper)new JavaScriptSerializer().Deserialize(json, typeof(Wrapper));
albuminfo.ItemsSource = w.albumdetail;
}
AlbumDetail class:
专辑详情类:
public class AlbumDetail
{
#region//private property
private int albumId;
private string albumName;
private DateTime releasedate;
private int productionId;
private string duration;
private string category;
private string themes;
private ImageBrush coverImage;
private ImageBrush profileImage;
private string description;
private double albumPrice;
private double specialPrice;
private int[] artistIds;
#endregion
#region//public property
public int[] ArtistIds
{
get { return artistIds; }
set { artistIds = value; }
}
public double SpecialPrice
{
get { return specialPrice; }
set { specialPrice = value; }
}
public double AlbumPrice
{
get { return albumPrice; }
set { albumPrice = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public ImageBrush ProfileImage
{
get { return profileImage; }
set { profileImage = value; }
}
public ImageBrush CoverImage
{
get { return coverImage; }
set { coverImage = value; }
}
public string Themes
{
get { return themes; }
set { themes = value; }
}
public string Category
{
get { return category; }
set { category = value; }
}
public string Duration
{
get { return duration; }
set { duration = value; }
}
public int ProductionId
{
get { return productionId; }
set { productionId = value; }
}
public DateTime Releasedate
{
get { return releasedate; }
set { releasedate = value; }
}
public string AlbumName
{
get { return albumName; }
set { albumName = value; }
}
public int AlbumId
{
get { return albumId; }
set { albumId = value; }
}
#endregion
}
and Wrapper class:
和包装类:
public class Wrapper
{
public List<AlbumDetail> albumdetail { get; set; }
}
The end result looks like below image: https://dl.dropboxusercontent.com/u/59201118/json.JPG(dropbox link because i'm not allowed to post image!)
最终结果如下图所示:https: //dl.dropboxusercontent.com/u/59201118/json.JPG(dropbox 链接,因为我不允许发布图片!)
回答by Stewart Mbofana
You should make the web request on a background thread and then update the UI using the dispatcherobject. I have modified your code to make it work. Here are the important bits,
您应该在后台线程上发出 Web 请求,然后使用调度程序对象更新 UI 。我已经修改了您的代码以使其正常工作。这是重要的部分,
private static readonly ManualResetEvent AllDone = new ManualResetEvent(false);
private void BtnLoadClick(object sender, RoutedEventArgs e)
{
var request =
(HttpWebRequest)
WebRequest.Create("http://coderomusic.com/beta/SecuredAdministrator/mobile/getInfo.php?_mod=album");
request.ContentType = "application/json";
request.BeginGetResponse(GetResponseCallback, request);
AllDone.WaitOne();
}
private void GetResponseCallback(IAsyncResult ar)
{
var request = (HttpWebRequest) ar.AsyncState;
var response = (HttpWebResponse) request.EndGetResponse(ar);
Stream stream = response.GetResponseStream();
Wrapper w = null;
if (stream != null)
{
var re = new StreamReader(stream);
string json = re.ReadToEnd();
w = (Wrapper) new JavaScriptSerializer().Deserialize(json, typeof (Wrapper));
}
Dispatcher.BeginInvoke(
new Action(() => {
if (w != null)
albuminfo.ItemsSource = new ObservableCollection<AlbumDetail>(w.AlbumDetail);
}));
response.Close();
AllDone.Set();
}
Change it to suit your needs. I hope it works.
更改它以满足您的需求。我希望它有效。
回答by user3196809
Simple and easy way use jsonormfrom nuget and create a data layer from received json by (http://json2csharp.com/)
简单和容易的方式利用jsonorm从创建的NuGet从接收JSON由数据层(和http://json2csharp.com/)

