简体   繁体   中英

Generate JSON object dynamically

I have a CSV which I am parsing to convert to JSON and then finally uploading to Azure blob.

This is an example of the CSV that I am reading and parsing. Each row will have its own JSON file.

humidity_sensor,    sensor1, {"temp":"22.3","batt":"3.11","ss":"28","humidity":"52.6","dp":"12.144704512672"}
humidity_sensor,    sensor1, {"batt":"3.14","ss":"16","humidity":"56.9","timestamp":1556568624,"temp":"21.7","dp":"12.784662018281"}
humidity_sensor,    sensor1, {"pressure":"5.14","prop2":"16","current":"56.9","temp":"21.7","dp":"12.784662018281"}

This is the model I want to serialize it to:

public class SensorModel
    {
        [JsonProperty("sensorId")]
        public string SensorId { get; set; }

        [JsonProperty("Inbound_data")]
        public Inbound Inbounddata { get; set; }
        [JsonProperty("ts")]
        public DateTime Ts { get; set; }
    }

    public class Inbound
    {
    }

So the output is of the following format:

   {
   "sensorId":"sensor1",
   "data_in":{

   },
   "ts":"2020-02-11T18:07:29Z"
}

The value in Inbound is the JSON from the CSV which is not constant and will change with each row of the CSV.

SensorModel sensorModel = new SensorModel 
                        {
                            SensorId = sensorId,
                            Ts = utcTimestamp,
                            Inbounddata  = new Inbound
                            {

                            }
                        };

But since I am not certain what is going to be in that node I can't define the properties in the Inbound class.

I tried using dynamic like this:

dynamic data = JObject.Parse(values[r, 4].ToString());

The right hand side of this expression is the value from CSV.

How can I dynamically figure out what properties are required under the inbound node. I could have simply updated the model to set the inbound property as JObject and then while creating the model assigned value to it but I need all the values of the inbound node to translate by looking up in the database.

Is there any way to achieve this?

You could declare Inbounddata as Dictionary<string,string>

public class SensorModel
{
    [JsonProperty("sensorId")]
    public string SensorId { get; set; }

    [JsonProperty("data_in")]
    public Dictionary<string,string> Inbounddata { get; set; }
    [JsonProperty("ts")]
    public DateTime Ts { get; set; }
}

For example,

var sensorModel = new SensorModel
{
    SensorId = "2",
    Ts = DateTime.Now,
    Inbounddata = new Dictionary<string,string>
    {
        ["temp"] = "22.5",
        ["batt"] = "3.11",
        ["ss"] = "22"
    }

};

var result = JsonConvert.SerializeObject(sensorModel);

Output

{"sensorId":"2","data_in":{"temp":"22.5","batt":"3.11","ss":"22"},"ts":"2020-02-24T20:46:39.9728582+05:30"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM