转换JSON对象为C#对象
问题描述:
请考虑下面的代码:转换JSON对象为C#对象
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;
using System.Net;
namespace wideeye4
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<ItemViewModel> Items { get; private set; }
private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its
value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
if (value != _sampleProperty)
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}
}
public bool IsDataLoaded
{
get;
private set;
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadData()
{
try
{
WebClient webclient = new WebClient();
Uri uri = new Uri(<http://192.168.100.100:3000/listings.json>);
webclient.OpenReadCompleted += new
OpenReadCompletedEventHandler(webclient_openreadcompleted);
webclient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void webclient_openreadcompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer serializer = null;
try
{
serializer = new
DataContractJsonSerializer(typeof(ObservableCollection<wideeye>));
e.Result.Position = 0;
var sr = new StreamReader(e.Result);
var json = sr.ReadToEnd();
ObservableCollection<wideeye> wideeyes = serializer.ReadObject(e.Result) as
ObservableCollection<wideeye>;
foreach (wideeye wd in wideeyes)
{
string varlisting_id = string.Empty;
string varlisting_image_file_name = string.Empty;
if (wd.listing != null)
{
string varcategory = wd.listing.category;
string varcity = wd.listing.city;
}
if (wd.listing_images != null)
{
varlisting_id = wd.listing_images.listing_id;
varlisting_image_file_name =
wd.listing_images.listing_image_file_name;
}
Items.Add(new ItemViewModel()
{ LineOne = wd.listing.category.ToString(),
LineTwo = wd.listing.city.ToString(),
LineThree =
string.Format("http://wideeye.hopto.org:3000/system/listing_images/{0}/medium/{1}",
varlisting_id, varlisting_image_file_name) });
}
this.IsDataLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
{
[DataContract]
public class wideeye
{
[DataMember]
public listing listing { get; set; }
[DataMember]
public listing_images listing_images { get; set; }
[DataMember]
public bids bids { get; set; }
[DataMember]
public messages messages { get; set; }
}
[DataContract]
public class listing
{
public string category { get; set; }
public string city { get; set; }
public string country { get; set; }
public string created_at { get; set; }
public string current_publish_date { get; set; }
public string details { get; set; }
public string id { get; set; }
public string industry { get; set; }
public string list_exp_date { get; set; }
public string list_price { get; set; }
public string list_start_date { get; set; }
public string make { get; set; }
public string model { get; set; }
public string open_bid { get; set; }
public string state { get; set; }
public string status { get; set; }
public string title { get; set; }
public string updated_at { get; set; }
public string year { get; set; }
}
[DataContract]
public class listing_images
{
public string created_at { get; set; }
public string id { get; set; }
public string listing_id { get; set; }
public string listing_image_content_type { get; set; }
public string listing_image_file_name { get; set; }
public string listing_image_file_size { get; set; }
public string listing_image_updated_at { get; set; }
public string updated_at { get; set; }
}
[DataContract]
public class bids
{
public string bid_exp_date { get; set; }
public string bid_price { get; set; }
public string bid_status { get; set; }
public string created_at { get; set; }
public string event_type { get; set; }
public string id { get; set; }
public string listing_id { get; set; }
public string msg_association_id { get; set; }
public string msg_text { get; set; }
public string msg_type { get; set; }
public string updated_at { get; set; }
public string user_id { get; set; }
}
[DataContract]
public class messages
{
public string bid_exp_date { get; set; }
public string bid_price { get; set; }
public string bid_status { get; set; }
public string created_at { get; set; }
public string event_type { get; set; }
public string id { get; set; }
public string listing_id { get; set; }
public string msg_association_id { get; set; }
public string msg_text { get; set; }
public string msg_type { get; set; }
public string updated_at { get; set; }
public string user_id { get; set; }
}
}
在webclient_openreadcompleted事件(e.result
)将返回MemoryStream
,所以它给人例外。由于我的JSON对象非常大,所以我面临很多问题。我为此使用了databoundWP7模型。
答
我不能肯定 - 你的代码示例是过于庞大......
但我认为这整个代码的结果读入一个字符串,然后尝试读取结果的第二次进入串行:
var sr = new StreamReader(e.Result);
var json = sr.ReadToEnd();
ObservableCollection<wideeye> wideeyes = serializer.ReadObject(e.Result) as
ObservableCollection<wideeye>;
如果这是真的,那么它将无法工作 - 你不能读取流两次
哪里抛出异常,什么是异常(这是一个进入网络流!)信息?如果你的json对象很大,有什么办法可以减小测试的大小吗? – earthling 2012-03-28 16:35:23