Home > Back-end >  How To Print Deserialized Key and Value MVC
How To Print Deserialized Key and Value MVC

Time:08-18

I have a JSON string that is being passed to a method that dynamically Deserializes all the values. I have a collection in the JSON string and once the method gets to that collection the value becomes "system.dynamic.expandoobject". However when I click the dropdown menu for the value it shows all the key and values again.

Here is my code in the back end.

            var config = JsonConvert.DeserializeObject<ExpandoObject>(m_decoded_string, new
            ExpandoObjectConverter());
            CellularNodeInfo cellularNode = new CellularNodeInfo();
            cellularNode.config = config;
            cellularNode.datetimestamp = m_timestamp_string;
            //Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(m_decoded_string);
            //int k = int.Parse("dsda"); //forcefully cause exception for testing.
            return View(cellularNode);

Here is my MVC code after parsing all values. Screenshot of the output

    @if (Model != null)
    {
        foreach (var d in Model.config)
        {
            <tr>
                <td>
                    @d.Key
                </td>
                <td>
                    @d.Value
                </td>

            </tr>

        }
        @Model.datetimestamp

    }

I attached an image of what it shows. As you can see Device Key shows value incorrectly. Here is the JSON String I am sending.

  {
         "JSON Version": 10,
         "Serial":1,
"Type": "Info",
"Device": {
    "Id": "60986714192368530242",
    "Type": "RCW-360Plus-THE",
    "Version": "4.70",
    "Battery": 90,
    "Bat Alarm":{
        "Low Power":false,
        "Power Plug Out":true
    },
    "Trip":"Start",
    
    "Network":{
        "Net Sys":"LTE",
        "Signal": 100,
        "ICCID":"898604A6102170638265",
        "IMEI":"863763056587362",
        "MCC":"460",
        "MNC":"0",
        "LAC":"9340",
        "CID":"45658883"
    },

    "Unit":"C",
    "Beep Alarm": true,
    "GPS":true,
    "GPS Period":5,
    "Collect Period": 5,
    "Upload Period": 5,
    "Alarm Collect Period": 2,
    "Alarm Upload Period": 2,
    "Delay Time":0,
    "Sensor1 Alarm Up Limit": 8.00,
    "Sensor1 Alarm Down Limit": 2.00,
    "Sensor2 Alarm Up Limit": 80.00,
    "Sensor2 Alarm Down Limit": 10.00,
    "Sensor3 Alarm Up Limit": 8.00,
    "Sensor3 Alarm Down Limit": 2.00,
    "Sensor4 Alarm Up Limit": 80.00,
    "Sensor4 Alarm Down Limit": 10.00
}

}

CodePudding user response:

If the input JSON String's format is fixed, you can create a class according to the format to replace ExpandoObject, and get the value through exact name, e.g. Model.config.Device.Id instead of using @d.Value in foreach loop.

Otherwise, if you do not know the input JSON String's format, here is another workaround.

First, create a partial view DynamicTable.cshtml

@model ExpandoObject
@using System.Dynamic;

<table>
    @foreach (var d in Model)
    {
        if (d.Value.GetType() == typeof(ExpandoObject))
        {
            <tr>
                <td>@d.Key</td>
                <td>
                    @Html.Partial("DynamicTable", d.Value)
                </td>
            </tr>
        }
        else
        {

            <tr>
                <td>
                    @d.Key
                </td>
                <td>
                    @d.Value
                </td>

            </tr>

        }
    }
</table>

Add Controller for the view

    public ActionResult DynamicTable(ExpandoObject data)
    {
        return View(data);
    }

At last, call the partial view from the original view, replacing the original table

    @if (Model != null)
    {
        @Html.Partial("DynamicTable", Model.config)
        @Model.datetimestamp

    }

This will automatically detects if the object is ExpandoObject, and use an additional foreach loop to show the values inside those nested objects.

As there are nested objects in the provided JSON, e.g. Device, Bat Alarm, Network. These would be converted to nested ExpandoObject in cellularNode.config, and they would be shown as "System.Dynamic.ExpandoObject" by default.

  • Related