Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON对象到C#对象的转换 请考虑以下代码: using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; u_C#_Windows Phone 7_Json.net - Fatal编程技术网

JSON对象到C#对象的转换 请考虑以下代码: using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; u

JSON对象到C#对象的转换 请考虑以下代码: using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; u,c#,windows-phone-7,json.net,C#,Windows Phone 7,Json.net,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.Doc

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; }
   }
}
使用系统;
使用系统组件模型;
使用System.Collections.Generic;
使用系统诊断;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
使用System.IO;
使用System.Xml.Linq;
使用System.Runtime.Serialization.Json;
Net系统;
命名空间wideeye4
{
公共类MainViewModel:INotifyPropertyChanged
{
公共主视图模型()
{
this.Items=新的ObservableCollection();
}
/// 
///ItemViewModel对象的集合。
/// 
公共可观测集合项{get;private set;}
私有字符串_sampleProperty=“示例运行时属性值”;
/// 
///示例ViewModel属性;此属性在视图中用于显示其
使用绑定的值
/// 
/// 
公共字符串SampleProperty
{
得到
{
返回sampleProperty;
}
设置
{
如果(值!=\u sampleProperty)
{
_样本属性=值;
NotifyPropertyChanged(“SampleProperty”);
}
}
}
公共布尔值已加载
{
得到;
私人设置;
}
/// 
///创建一些ItemViewModel对象并将其添加到Items集合中。
/// 
公共void LoadData()
{
尝试
{
WebClient WebClient=新的WebClient();
Uri=新Uri();
webclient.OpenReadCompleted+=新建
OpenReadCompletedEventHandler(webclient\u openreadcompleted);
OpenReadAsync(uri);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}       
}
void webclient\u openreadcompleted(对象发送方,OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer序列化程序=null;
尝试
{
序列化程序=新建
DataContractJsonSerializer(类型(ObservableCollection));
e、 结果:位置=0;
var sr=新的StreamReader(如结果);
var json=sr.ReadToEnd();
ObservableCollection wideeyes=serializer.ReadObject(e.Result)作为
可观察收集;
foreach(宽眼中的宽眼wd)
{
string varlisting_id=string.Empty;
string varlisting\u image\u file\u name=string.Empty;
if(wd.listing!=null)
{
字符串varcategory=wd.listing.category;
字符串varcity=wd.listing.city;
}
if(wd.u图像!=null)
{
varlisting_id=wd.listing_images.listing_id;
varlisting_image_file_name=
wd.listing_images.listing_image_file_name;
}
添加(新的ItemViewModel()
{LineOne=wd.listing.category.ToString(),
LineTwo=wd.listing.city.ToString(),
第三行=
字符串格式(“http://wideeye.hopto.org:3000/system/listing_images/{0}/medium/{1}“,
varlisting_id,varlisting_image_file_name)});
}
this.IsDataLoaded=true;
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
{
[数据合同]
公共级宽带眼
{
[数据成员]
公开列表{get;set;}
[数据成员]
公共列表\u图像列表\u图像{get;set;}
[数据成员]
公开投标{get;set;}
[数据成员]
公共消息{get;set;}
}
[数据合同]
公共类列表
{
公共字符串类别{get;set;}
公共字符串city{get;set;}
公共字符串国家{get;set;}
在{get;set;}处创建的公共字符串
公共字符串当前发布日期{get;set;}
公共字符串详细信息{get;set;}
公共字符串id{get;set;}
公共字符串行业{get;set;}
公共字符串列表\u exp\u日期{get;set;}
公共字符串列表\u价格{get;set;}
公共字符串列表\u开始\u日期{get;set;}
公共字符串make{get;set;}
公共字符串模型{get;set;}
公共字符串open_bid{get;set;}
公共字符串状态{get;set;}
公共字符串状态{get;set;}
公共字符串标题{get;set;}
在{get;set;}处更新了公共字符串
公共字符串年份{get;set;}
}
[数据合同]
公共类列表\u图像
{
在{get;set;}处创建的公共字符串
公共字符串id{get;set;}
公共字符串列表_id{get;set;}
公共字符串列表\图像\内容\类型{get;set;}
公共字符串列表\图像\文件\名称{get;set;}
公共字符串列表\图像\文件\大小{get;set;}
在{get;set;}处列出_image_updated_的公共字符串
在{get;set;}处更新了公共字符串
}
[数据合同]
公开课投标
{
公共字符串bid_exp_date{get;set;}
公共字符串bid_price{get;set;}
公共字符串bid_status{get;set;}
在{get;set;}处创建的公共字符串
公共字符串事件类型{get;set;}
公共字符串id{get;set;}
公共字符串列表_id{get;set;}
公共字符串msg_association_id{get;set;}
公共字符串msg_text{get;set;}
公共stri
        var sr = new StreamReader(e.Result);
        var json = sr.ReadToEnd();
        ObservableCollection<wideeye> wideeyes = serializer.ReadObject(e.Result) as 
             ObservableCollection<wideeye>;