如何阅读 json 提要的第一部分 - Windows Phone [英] How to to read the first part of the json feed - Windows Phone

查看:31
本文介绍了如何阅读 json 提要的第一部分 - Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了一段时间,我可以阅读最后一部分,所以从方括号到结尾的所有内容.但在那之前我什么也读不懂,当我把它放进去时,我得到了一个 NullExceptionReference.

I have been working on this for some time, i can read the last part, so everything from the square bracket to the end. But i can't read anything before that, when i put it in i Get a 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); 
        }

    }
}
}

这是提要结果

{
"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":""
 }
 ]}

那么我需要在代码中做什么修改才能让我能够阅读代码的第一部分.

So what do i need to do modify in the code in order for me to be able to read the first part of the code.

 {
"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

推荐答案

你需要有一个 ObservableCollection 的容器类来收集所有的数据!

You need to have a container class for the ObservableCollection<Employee> as to gather all that data!

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 写的更改,希望我没有犯任何错误...)

(I wrote the changes with Nodepad, hope I didn't make any error...)

这篇关于如何阅读 json 提要的第一部分 - Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆