Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derserialize JSON Object from Firebase in C#

Tags:

json

c#

firebase

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.

like image 842
John Dougherty Avatar asked Dec 17 '15 12:12

John Dougherty


People also ask

How to serialize and deserialize JSON?

JavaScriptSerializer is a class that helps to serialize and deserialize JSON. It is present in the namespace System.Web.Script.Serialization is available in assembly System.Web.Extensions.dll. To serialize a .Net object to JSON string use the Serialize method.

Why can’t I use jsonelement dynamic with the native deserializer?

Because, under the hood, this is a boxed JsonElement, a type that is the building block of native JSON DOM. So, we don’t have the convenience to use it in a truly dynamicway. In short, usingdynamicwith the native deserializer has no added benefit and results in a JSON DOM which has its own API to deal with.

How to upload JSON file to Firebase Realtime Database?

First of all Configure your Firebase project for the web How to Configure Google Firebase for Web Now click on 3 dots and select import JSON, select your file. Ahhan! your JSON file is now uploaded to Firebase Realtime Database. To see this as an API, Copy the URL of the database and write the folder name .json after it.

How to deserialize a JSON input with more nesting levels than allowed?

If your JSON input contains more nesting levels than allowed, you can pass an extra parameter of type DeserializationOption::NestingLimit to deserializeJson (). See the example below.


2 Answers

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.

like image 151
Berin Loritsch Avatar answered Oct 19 '22 20:10

Berin Loritsch


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));
like image 5
Alex Avatar answered Oct 19 '22 20:10

Alex