i am trying to parse a JSON file and am having major problems with the above the device says "Object reference not set to an instance of an object." so I'm lost.
here is my code
mypage.xaml.cs
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<User>));
ObservableCollection<User> User = ser.ReadObject(e.Result) as ObservableCollection<User>;
foreach (User em in User)
{
txbName.Text = "Username: " + em.Username;
txbFirstName.Text = "FirstName:" +em.FirstName;
txbSurname.Text ="Surname: " +em.Surname ;
txbEmail.Text = "Email: " + em.Email;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnGetData_Click(object sender, EventArgs e)
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://beta.cloud-education.cu.cc/api/User/1");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
User.cs
class User
{
public int id {get; set;}
public string Username {get; set;}
public string FirstName {get; set;}
public string Surname {get;set;}
public string Email {get;set;}
public string LiveId { get; set; }
public int Language { get; set; }
public int Subjects { get; set; }
}
i just can not see where I'm going wrong the uri is correct and the output of the JSON is
{"Id":1,"Username":"Test1","Firstname":"Fir1","Surname":"Sur1","Email":"Test1@contoso.com","LiveId":"LID1","Language":"1","Subjects":"1"}
UPDATE- i didnt realize that one of the strings was not set right but still same error UPDATE 2 - John when i run the app i get this from the intermediate window
A first chance exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL An exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL and wasn't handled before a managed/native boundary A first chance exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL An exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL and wasn't handled before a managed/native boundary
and this is also displayed after that addition
System.NullReferenceException: Object reference not set to an instance of an object.
at c_Sharp_WP8_Clo_Edu.viewinfo.webClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
Update 3 - i have scanned everywhere i can see but with no success. Update 4 - so i implimented the new code and got this error on the phone Type'c_sharp_WP8_Clou_Edu.User' cannot be serialized. Consider making it with the DataContractAttribute attribute, and marking all of its members you want serialized with the SataMemberAttribute attribute. Alturnatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialied and no attributes will be reqired.
this is a MAJOR forward move i feel so any more help please please let me know