我一直在研究这个问题,我可以阅读最后一部分,所以从方括号到结尾都是如此。但是在我之前我无法阅读任何内容,当我把它放入我的NullExceptionReference时。
这是我的代码:
namespace WP7_ConsumeJSON
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myButton.Click += new RoutedEventHandler(myButton_Click);
}
void myButton_Click(object sender, RoutedEventArgs e)
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://www.ournu.co.uk/list.txt");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<Employee>));
ObservableCollection<Employee> employees = ser.ReadObject(e.Result) as ObservableCollection<Employee>;
foreach (Employee em in employees)
{
string id = em.ServiceName;
string dt = em.Destination;
string tm = em.DepartureTimeAsString;
listBoxService.Items.Add(id);
listBoxDestination.Items.Add(dt);
listBoxTime.Items.Add(tm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
这是Feed结果
{
"StopName":"Race Hill",
"stopId":7553,
"NaptanCode":"bridwja",
"LongName":"Race Hill",
"OperatorsCode1":"07645",
"OperatorsCode2":"7645",
"OperatorsCode3":"50701",
"OperatorsCode4":"bridwja",
"Departures":[
{
"ServiceName":"22",
"Destination":"Churchill Sq",
"DepartureTimeAsString":"1 min",
"DepartureTime":"01/01/0001 00:00:00",
"Notes":""
}
,
{
"ServiceName":"37",
"Destination":"Bristol Estate",
"DepartureTimeAsString":"1 min",
"DepartureTime":"01/01/0001 00:00:00",
"Notes":""
}
]}
那么我需要在代码中修改什么才能让我能够阅读代码的第一部分。
{
"StopName":"Race Hill",
"stopId":7553,
"NaptanCode":"bridwja",
"LongName":"Race Hill",
"OperatorsCode1":"07645",
"OperatorsCode2":"7645",
"OperatorsCode3":"50701",
"OperatorsCode4":"bridwja",
"Departures":[
员工类的结构:
namespace WP7_ConsumeJSON
{
public class Employee
{
public string StopName { get; set; }
public int stopId { get; set; }
public string NaptanCode { get; set; }
public string LongName { get; set; }
public string OperatorsCode1 { get; set; }
public string OperatorsCode2 { get; set; }
public string OperatorsCode3 { get; set; }
public string OperatorsCode4 { get; set; }
public string ServiceName { get; set; }
public string Destination { get; set; }
public string DepartureTimeAsString { get; set; }
public string DepartureTime { get; set; }
public string Notes { get; set; }
}
}
下载项目 - WP7_JSON.zip
答案 0 :(得分:0)
您需要拥有ObservableCollection<Employee>
的容器类才能收集所有数据!
public class RootContainer
{
[DataMember]
public string StopName { get; set; }
[DataMember]
public int stopId { get; set; }
[DataMember]
public string NaptanCode { get; set; }
//Other root properties go here
[DataMember]
public ObservableCollection<Employee> Employees { get; set; }
}
然后像这样更改代码:
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(RootContainer));
RootContainer rootContainer = ser.ReadObject(e.Result) as RootContainer;
foreach (Employee em in rootContainer.Employees)
{
string id = em.ServiceName;
string dt = em.Destination;
string tm = em.DepartureTimeAsString;
listBoxService.Items.Add(id);
listBoxDestination.Items.Add(dt);
listBoxTime.Items.Add(tm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
(我用Nodepad写了这些变化,希望我没有犯任何错误......)