Я работал над этим в течение некоторого времени, я могу прочитать последнюю часть, так что все от квадратной скобки до конца.Но я не могу ничего прочитать до этого, когда я вставляю это, я получаю 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":""
}
]}
Так что мне нужно изменить в коде, чтобычтобы я мог прочитать первую часть кода.
{
"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