当我有嵌套对象时如何反序列化JSON?

时间:2022-09-15 12:36:42

JSON response string send by client:

客户端发送的JSON响应字符串:

{  "{\"user_id\":\"\",\"school_id\":\"1\",\"user_name\":\"pfirstname1@example.com\",\"firstname\":\"pfirstname1\",\"surname\":\"psurname1\",\"type\":\"P\",\"phoneno\":\"1446863\",\"mobileno\":\"3614632\",\"address\":\"mehdipatnam\",\"relation\":\"Father\",\"date_created\":\"\",\"student_dto\":": {    "{\"student_id\":0,\"user_id\":0,\"firstname\":\"sfirstname1\",\"surname\":\"ssurname1\",\"rollno\":1,\"class_id\":\"2\",\"school_id\":\"1\",\"address\":\"mehdipatnam\",\"remarks\":\"\",\"date_created\":\"\"}]}": ""  }}

Then to deserialize this I'm doing this step:

然后反序列化这个我正在做这个步骤:

dynamic data = new JavaScriptSerializer().Deserialize(response.ToString(), typeof(object));

Then when I look in to the debugger I can see:

然后,当我查看调试器时,我可以看到:

[0] {[{"user_dto":{"user_id":"","school_id":"1","user_name":"pfirstname1@example.com","firstname":"pfirstname1","surname":"psurname1","type":"P","phoneno":"1446863","mobileno":"3614632","address":"mehdipatnam","relation":"Father","date_created":"","student_dto":, System.Collections.Generic.Dictionary`2[System.String,System.Object]]}  System.Collections.Generic.KeyValuePair<string,object>

But how do I access the values?

但是我如何访问这些值?

1 个解决方案

#1


I'm going into this assuming that the mess you posted as JSON should actually be the following after all the \" and erroneous quotes are removed.

假设在删除了所有“错误”引号之后,你发布的JSON信息实际上应该是以下内容。

{
    "user_id":"",
    "school_id":"1",
    "user_name":"pfirstname1@example.com",
    "firstname":"pfirstname1",
    "surname":"psurname1",
    "type":"P",
    "phoneno":"1446863",
    "mobileno":"3614632",
    "address":"mehdipatnam",
    "relation":"Father",
    "date_created":"",
    "student_dto":
    {
        "student_id":0,
        "user_id":0,
        "firstname":"sfirstname1",
        "surname":"ssurname1",
        "rollno":1,
        "class_id":"2",
        "school_id":"1",
        "address":"mehdipatnam",
        "remarks":"",
        "date_created":""
    }
}

If that's the case, then json2csharp.com gives the following as the classes to deserialize into:

如果是这种情况,则json2csharp.com将以下内容作为要反序列化的类:

public class StudentDto
{
    public int student_id { get; set; }
    public int user_id { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public int rollno { get; set; }
    public string class_id { get; set; }
    public string school_id { get; set; }
    public string address { get; set; }
    public string remarks { get; set; }
    public string date_created { get; set; }
}

public class RootObject
{
    public string user_id { get; set; }
    public string school_id { get; set; }
    public string user_name { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public string type { get; set; }
    public string phoneno { get; set; }
    public string mobileno { get; set; }
    public string address { get; set; }
    public string relation { get; set; }
    public string date_created { get; set; }
    public StudentDto student_dto { get; set; }
}

Now you should be able to deserialize directly into that type.

现在,您应该能够直接反序列化为该类型。

new JavaScriptSerializer().Deserialize<RootObject>(response.ToString());

#1


I'm going into this assuming that the mess you posted as JSON should actually be the following after all the \" and erroneous quotes are removed.

假设在删除了所有“错误”引号之后,你发布的JSON信息实际上应该是以下内容。

{
    "user_id":"",
    "school_id":"1",
    "user_name":"pfirstname1@example.com",
    "firstname":"pfirstname1",
    "surname":"psurname1",
    "type":"P",
    "phoneno":"1446863",
    "mobileno":"3614632",
    "address":"mehdipatnam",
    "relation":"Father",
    "date_created":"",
    "student_dto":
    {
        "student_id":0,
        "user_id":0,
        "firstname":"sfirstname1",
        "surname":"ssurname1",
        "rollno":1,
        "class_id":"2",
        "school_id":"1",
        "address":"mehdipatnam",
        "remarks":"",
        "date_created":""
    }
}

If that's the case, then json2csharp.com gives the following as the classes to deserialize into:

如果是这种情况,则json2csharp.com将以下内容作为要反序列化的类:

public class StudentDto
{
    public int student_id { get; set; }
    public int user_id { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public int rollno { get; set; }
    public string class_id { get; set; }
    public string school_id { get; set; }
    public string address { get; set; }
    public string remarks { get; set; }
    public string date_created { get; set; }
}

public class RootObject
{
    public string user_id { get; set; }
    public string school_id { get; set; }
    public string user_name { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public string type { get; set; }
    public string phoneno { get; set; }
    public string mobileno { get; set; }
    public string address { get; set; }
    public string relation { get; set; }
    public string date_created { get; set; }
    public StudentDto student_dto { get; set; }
}

Now you should be able to deserialize directly into that type.

现在,您应该能够直接反序列化为该类型。

new JavaScriptSerializer().Deserialize<RootObject>(response.ToString());