简体   繁体   English

解析json时出现system.nullreferenceException

[英]system.nullreferenceexception when parsing json

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." 我正在尝试解析JSON文件,并且上面的设备存在严重问题,设备显示“对象引用未设置为对象的实例”。 so I'm lost. 所以我迷路了。

here is my code 这是我的代码

mypage.xaml.cs 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 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 我只是看不到我要去哪里错,uri是正确的,JSON的输出是

{"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 更新-我没有意识到其中一个字符串设置不正确,但仍然存在相同的错误。更新2-约翰,当我运行应用程序时,我从中间窗口获取了此信息。

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 类型'System.NullReferenceException'的第一次机会异常发生在c_Sharp_WP8_Clo_Edu.DLL中。类型'System.NullReferenceException'的异常发生在c_Sharp_WP8_Clo_Edu.DLL中,并且在托管/本地边界类型'System'之前没有被处理。在C_Sharp_WP8_Clo_Edu.DLL中发生了NullReferenceException'

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) 在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. 更新3-我扫描了我能看到的所有地方,但没有成功。 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. 更新4-因此我隐含了新代码,并在电话类型为“ c_sharp_WP8_Clou_Edu.User”的手机上出现此错误。无法对用户进行序列化。 Consider making it with the DataContractAttribute attribute, and marking all of its members you want serialized with the SataMemberAttribute attribute. 考虑使用DataContractAttribute属性进行标记,并使用SataMemberAttribute属性标记要序列化的所有成员。 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 我觉得这是一个重大的进步,因此,如果需要其他帮助,请告诉我

Your JSON data is not an array, it's a singular object, so when you try to deserialize to an ObservableCollection, it just returns null. 您的JSON数据不是数组,而是单个对象,因此当您尝试反序列化为ObservableCollection时,它仅返回null。 Try this code instead 试试这个代码

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    DataContractJsonSerializer ser = null;
    try
    {
        ser = new DataContractJsonSerializer(typeof(User));
        var user = ser.ReadObject(e.Result) as User;
        txbName.Text = "Username: " + user.Username;
        txbFirstName.Text = "FirstName:" + user.FirstName;
        txbSurname.Text ="Surname: " + user.Surname;
        txbEmail.Text = "Email: " + user.Email;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM