B
B
BloodyBlade2017-09-15 11:18:37
JSON
BloodyBlade, 2017-09-15 11:18:37

How to generate json like this?

Hello everyone, I was faced with the task of generating json in the following format:

{
    "#DATE#": {
        "#TIME#": {
            "FREE": "#FREE_VALUE#",
            "PRICE": "PRICE_VALUE"
        },
        ...
        "#TIME#": {
            "FREE": "#FREE_VALUE#",
            "PRICE": "PRICE_VALUE"
        }
    },
    ...
    "#DATE#": {
        "#TIME#": {
            "FREE": "#FREE_VALUE#",
            "PRICE": "PRICE_VALUE"
        },
        ...
        "#TIME#": {
            "FREE": "#FREE_VALUE#",
            "PRICE": "PRICE_VALUE"
        }
    }
}

where
#DATE# (string) - date in the format DD.MM.YYYY (example 03/08/2017)
#TIME# (string) - slot time (example 12:00)
#FREE_VALUE# (boolean) - shows whether it is free/busy time (example true)
#PRICE_VALUE# (int) - indicates the price at this time (example 2500).
Example of resulting JSON:
{
        "08.03.2017": {
            "01:30":{
                "FREE":true,
                "PRICE":3500
             },
            "18:00":{
                "FREE":true,
                "PRICE":3500
            },
            "21:00":{
                "FREE":false,
                "PRICE":3500
            }
        },
        "09.03.2017": {
            "01:30":{
                "FREE":true,
                "PRICE":3500
             },
            "18:00":{
                "FREE":true,
                "PRICE":3500
            },
            "21:00":{
                "FREE":false,
                "PRICE":3500
            }
        },
        "10.03.2017": {
            "01:30":{
                "FREE":true,
                "PRICE":3500
             },
            "18:00":{
                "FREE":true,
                "PRICE":3500
            },
            "21:00":{
                "FREE":false,
                "PRICE":3500
            }
        }
    }

For serialization, I usually use the newtonsoft.json library and now I can’t figure out what kind of class structure I need to make in order to serialize it into such json? As far as I understand, there are only properties, no collections.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2017-09-15
@BloodyBlade

I think that dictionaries (Dictionary) are ideal for you
https://www.newtonsoft.com/json/help/html/Serializ...
Something like:

Dictionary<string, Dictionary<string, SomeObject>> a;
a["10.03.2017"]["01:30"] = new SomeObject{FREE = true, PRICE=1234}

D
d-stream, 2017-09-15
@d-stream

Easiest and fastest option for VS: www.c-sharpcorner.com/article/how-to-paste-json-as...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question