Derserialize JSON Object from Firebase in C#

firebase filter by child value
json firebase
how to get data from firebase database using php
firebase get data by key javascript
firebase snapshot to json
retrieve data from firebase c#
firebase retrieve data
datasnapshot to json

I am querying Firebase and retrieve a collection of objects like so:

{"-K5f0ccEKkVkxTAavQKY": {
  "Appeal": {
    "ID": "1450273330435",
    "comps": [
      162248,
      162272,
      162273,
      162281,
      162544
    ],
    "property": {
      "Address": "15 Main Street",
      "propID": 169729
    },
    "timeDateStamp": "Wed Dec 16 2015 08:42:10 GMT-0500 (Eastern Standard Time)",
    "userUUID": "google:229139952703238437512",
    "year": 2016
  }
}}

I would like to deserialize them into objects with this definition:

public class Appeal
{
    public string ID;
    public List<string> comps;
    public AppealProperty property;
    public string timeDateStamp;
    public string UUID;
    public int year;
}

public class AppealProperty
{
    public string address;
    public string propID;
}

I have troubles getting it deserialized. I don't need the initial string (e.g. "K5f0ccEKkVkxTAavQKY"). I'm able to change the object definitions if need be. I have a feeling a Dictionary would be useful.

The quick and dirty object is to use Dictionary<string,Appeal> as your deserialization target. At that point it would be as simple as:

var firebaseLookup = JsonConvert.DeserializeObject<Dictionary<string,Appeal>>(json);
var data = firebaseLookup.Values.ToList(); // or FirstOrDefault();

This approach would also handle the case if you ever had to get multiple objects at once, and it would give you the opportunity to use that key if it turns out the key was important after all.

Deserialize response from firebase, I have been using Newtonsoft for most of my JSON handling, will I need to use a different library? UPDATE: the names of the objects in the JSON  Those methods also make using JSON to serialize and deserialize objects easy in practice. This entry was posted in algorithms , serialization and tagged algorithms , C# , C# programming , deserialization , deserialize , example , example program , JavaScript Object Notation , JSON , objects , serialization , serialize , Windows Forms programming .

You could serialise your data into the classes below.

public class AppealProperty
{
    public string Address { get; set; }
    public int propID { get; set; }
}

public class Appeal
{
    public string ID { get; set; }
    public List<int> comps { get; set; }
    public AppealProperty property { get; set; }
    public string timeDateStamp { get; set; }
    public string userUUID { get; set; }
    public int year { get; set; }
}


public class FireBase
{
    public Appeal Appeal { get; set; }
}

public class RootObject
{
    [JsonProperty(PropertyName = " - K5f0ccEKkVkxTAavQKY")]
    public FireBase FireBaseRoot
    {
        get;
        set;
    }
}

Assuming that you are using JSON.NET, you can then get the object you are after, using this snippet:

var firebaseObject = JsonConvert.DeserializeObject<RootObject>(json);
var data = firebaseObject.FireBaseRoot.Appeal;

If the root name is dynamic, as indicated by your comment, you could skip the root instead and serialise straight into the FireBase class:

JObject parsedJson = JObject.Parse(json);
var fireBase = parsedJson.First.Children().First().ToObject(typeof (FireBase));

Retrieving Data, I'm getting the following error: Cannot deserialize the current JSON array (e.g. [1,​2 Demp]' because the type requires a JSON object (e.g. {"na I tried to import them in Firebase console without including them in an array.)  Serialization. In Serialization, it converts a custom .Net object to a JSON string. In the following code, it creates an instance of BlogSiteclass and assigns some values to its properties. Then we create an instance of JavaScriptSerializer and call Serialize () method by passing object (BlogSites).

Since I've never been able to parse a DataSnapshot with newtonSoft Json parser, I did this to build a list of object I needed to put in a ListView:

MyModelObject class

    public class MyModelObject: Java.Lang.Object
{
    public string Title { get; set; }
    public string Description { get; set; }

    public MyModelObject(){}
}

into My Listener

        public void OnDataChange(DataSnapshot snapshot)
    {
        List<MyModelObjecct> myList = new List<MyModelObject>();
        myList = databaseService
            .GetMyModelObjectList(snapshot
            .Children?
            .ToEnumerable<DataSnapshot>());
    }

Method into the DatabaseService class

        public List<MyModelObject> GetMyModelObjectList(IEnumerable<DataSnapshot> enumerableSnapshot)
    {
        List<MyModelObject> list = new List<MyModelObject>();

        foreach (var item in enumerableSnapshot)
        {
            list.Add(ObjectExtensions.DataSnapshotToObject<MyModelObject>(item.Children?.ToEnumerable<DataSnapshot>()));
        }

        return list;
    }

ObjectExtensions class

      public static class ObjectExtensions
      {
        public static T DataSnapshotToObject<T>(IEnumerable<DataSnapshot> source)
        where T : class, new()
        {
        var someObject = new T();
        var someObjectType = someObject.GetType();

        foreach (var item in source)
        {
            someObjectType
                     .GetProperty(item.Key)
                     .SetValue(someObject, item.Value.ToString(), null);
        }

        return someObject;
    }
}

Cannot deserialize the current JSON array · Issue #21 · step-up-labs , Overview: This code snippet demonstrates how to read firebase collections. The returning firebase JSON string is assigned as dynamic json. The response. The quick and dirty object is to use Dictionary<string,Appeal> as your deserialization target. At that point it would be as simple as: At that point it would be as simple as: var firebaseLookup = JsonConvert.DeserializeObject<Dictionary<string,Appeal>>(json); var data = firebaseLookup.Values.ToList(); // or FirstOrDefault();

C# How to deserialize a firebase json into a List collection of objects, using JsonUtility. I have a json from Firebase DataSnapShot using GetRawJsonValue. I have trouble converting it into object using JsonUtility. You can use my SimpleJSON to parse arbitrary JSON data. Note that your  Deserialize an Object. Deserialize a Collection. Deserialize JSON from a file. Populate an Object. ConstructorHandling setting. ObjectCreationHandling setting.

Firebase Datasnapshot Json to Object using JsonUtility, Demonstrates how to read parts of a Firebase JSON database. // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock  How to serialize and deserialize Json objects in C#. A simple console app to demonstrate this. After creating your Visual Studio application, console or otherwise, make sure the System.Web.Extension reference is added:

C# Firebase GET, Firebase provides many SDKs for different platforms as well as it provides REST API to interact with database. We are writing this data to root node of our database JSON tree. and appending to RootNode and returns FirebaseDB object with combined URI. Parse(inJSON);; output = parsedJSON. JSON is a faster and more lightweight data exchange pattern between servers and the clients. Let us see in a demo how to convert an object to JSON and JSON Text into a C# object. Nuget provides a plug-in called JSON.NET which provides the facility to convert this.

Comments
  • How are you assigning the data read in from Firebase into your c# class? i.e. are you reading in a snapshot and assigning it's data to the Appeal class properties?
  • That's a good idea, but I should have mentioned that the property name " - K5f0ccEKkVkxTAavQKY" isdifferent for each object.
  • See the end of my updated answer. You can use JObject.Parse from the JSON.NET library to skip the root.