【问题标题】:JSON data mapping with C# class modelsJSON 数据与 C# 类模型的映射
【发布时间】:2017-08-06 16:00:00
【问题描述】:

我有一个 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 中有一个匹配的属性。但是我的报告模型没有被填充,因为它有一些没有被模型映射的动态属性。

请告诉我哪里出了问题以及可能的解决方案。

提前致谢。

【问题讨论】:

  • 你用什么来反序列化 JSON ? JsonDataContract 还是 JSON.net ?
  • 您没有说您使用的是什么序列化程序,但大多数 JSON 序列化程序都支持将带有自定义键的 JSON 对象反序列化为 Dictionary&lt;string, X&gt;。那应该做你需要的。见Create a strongly typed c# object from json object with ID as the name
  • 我正在使用 Firesharp 库(用于 .net)从 firebase 数据库中获取数据。它有自己的反序列化器。下面是语法`var response = FirebaseClient.Get("users"); var result = response.ResultAs>();

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


【解决方案1】:

按照 dbc 的建议,您应该修改您的模型。 删除reports 类并修改User 类如下:

public class User
{
    public user_info user_info { get; set; }
    public Dictionary<string, reportInfo> reports { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 2015-05-29
    • 2012-06-16
    相关资源
    最近更新 更多