gooddereklu
gooddereklu

Reputation: 1

How to convert downloaded document object into usable JSON

I'm working on a function where will able to download Firestore with a caching system using webrequest and E-tag comparison.

I've successfully complete the local caching system and will record every download data, so that next time you want to download the same data, it will check past downloaded record and compare whether firestore data is modified.

The problem is by using firestore's REST API and webrequest, I ended up retrieve a data that looks like this:

As you can see, the result is troublesome. I could not transfer it by using json format because it's a Firestore Document object. All I know that I can transfer it into a Dictionary<string,object>, but my Class object has more than two levels, the structure of this example is like Class > John > John's personal data, yet each level all contains the key 'field'. Is there any way that I can convert these line into the object I used in C# project?

Upvotes: 0

Views: 263

Answers (1)

Undiscouraged
Undiscouraged

Reputation: 1125

You can do the following steps:

  1. paste the JSON document from your screenshot to https://json2csharp.com/ to generate DTO classes. (replace all the sensitive data inside the document first - only the structure is required!)

  2. refine the generated DTO classes to your needs

  3. use Newtonsoft.JSON to deserialize the JSON document into objects of the generated classes

Alternatively you can deserialize the JSON document into a "dynamic" using Newtonsoft.JSON and access all the internals of the document from your code. This might be a good way if the structure of the original document must be very flexible.

Upvotes: 1

Related Questions