使用C#类模型进行JSON数据映射

时间:2016-12-19 07:04:59

标签: c# json firebase firebase-realtime-database nosql

我有一个noSQL数据库。当我尝试使用C#类模型映射JSON数据时,我的一些数据会被映射,但有些数据不会被映射。

以下是我的样本JSON格式。

{
    "J1D0GhKmzAT4gRn5VkfPKKVCfku2": {
        "reports": {
            "-KYbi7tbJoZJmCs8hcHy": {
                "age": "0",
                "description": "Test",
                "incident": "Test",
                "location": "Test"
            },
            "-KYbmoWJwzSSS0llsSZN": {
                "age": "0",
                "description": "Test",
                "incident": "Test",
                "location": "Test"
            },
            "-KYbszjzkYnH2N9xbFMJ": {
                "age": "0",
                "description": "Test",
                "incident": "Test",
                "location": "Test"
            }
        },
        "user_info": {
            "dob": "Feb 11, 2016",
            "name": "Test",
            "phone": "44444",
            "sex": "Male",
            "work": "llllll"
        }
    },
    "JxmpIWWioFbg1Po4gXtV07pwDvX2": {
        "reports": {
            "-KYiiDRl7fYAPdav13h3": {
                "age": "0",
                "description": "Test",
                "incident": "Test",
                "location": "Test"
            },
            "-KYinWZeP7N24x8QUC6O": {
                "age": "0",
                "description": "Test",
                "incident": "Test",
                "location": "Test"
            }
        },
        "user_info": {
            "dob": "Feb 11, 2016",
            "name": "Test",
            "phone": "44444",
            "sex": "Male",
            "work": "llllll"
        }
    }
}

和我的C#类模型如下

public class User
{
    public user_info user_info { get; set; }
    public reports reports { get; set; }    
}
public class user_info
{
    public string dob { get; set; }       
    public string name { get; set; }
    public string phone { get; set; }
    public string sex { get; set; }
    public string work { get; set; }
}
public class reports 
{
    public List<reportInfo> reportInfo { get; set; }
}    
public class reportInfo
{
    public string age { get; set; }
    public string description { get; set; }
    public string incident { get; set; }
    public string location { get; set; } 
}

这里,当我尝试使用C#类映射JSON时,由于某种原因只会填充user_info模型。 JSON中有匹配的属性。但是我的报告模型没有填充,因为它有一些动态属性,不能用模型映射。

请告诉我出错的地方和可能的解决方案。

提前致谢。

1 个答案:

答案 0 :(得分:0)

dbc 建议之后,您应该修改您的模型。 删除<DefaultProjectNameSpace>类并修改reports类,如下所示:

User