0

I have a noSQL database. When I'm trying to map the JSON data with C# class models, some of my data get mapped, but some don't.

Below is my Sample JSON format.

{
    "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"
        }
    }
}

and My C# Class Models are as below

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; } 
}

Here, when I try to map the JSON with C# classes, only the user_info model gets populated for some reason. There is a matching property in JSON. But my reports model doesn't get populated, because it has some dynamic properties which is not getting mapped with model.

Please let me know where I am going wrong and the possible solution.

Thanks in advance.

Community
  • 1
  • 1
  • 1
    What do you use to deserialize the JSON ? JsonDataContract or JSON.net ? – Arnaud Develay Dec 19 '16 at 08:26
  • 1
    You don't say what serializer you are using, but most JSON serializers support deserializing a JSON object with custom keys as a `Dictionary`. That should do what you need. See [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/questions/34213566). – dbc Dec 19 '16 at 09:06
  • I'm using Firesharp library(for .net) to get data from firebase database. And it having it's own deserializer. Below is the Syntax ` var response = FirebaseClient.Get("users"); var result = response.ResultAs>(); – Salil Saini Dec 19 '16 at 09:16

1 Answers1

0

Following dbc advice, you should modify your model. Delete the reports class and modify User class like this:

public class User
{
    public user_info user_info { get; set; }
    public Dictionary<string, reportInfo> reports { get; set; }
}
Arnaud Develay
  • 3,920
  • 2
  • 15
  • 27