How to deserialize a json in .net core 3.0

房东的猫 提交于 2019-12-11 07:28:06

问题


I have been facing some trouble to deserialize a json using .Net Core 3.0 Blazor app.

The Framework i am using .Net Core 3.0 Blazor with Visual Studio Preview 2019

{
    "cols": [
        "ID",
        "LastName",
        "Firstname",
        "middlename",
        "Suffix",
        "Title"

    ],
    "rows": [
        [
            "90",
            "Dada",
            "Mama",
            "",
            "",
            ""

        ]
    ]
}

Expected result: i want to load this json dynamically as a table in the web form using blazor. I don't mind to use any UI framwork for this like devexpress or telerik.

id LastNmae Firstname middlename Suffix Title

90 Dada Mama

Expected Result


回答1:


Steps accomplish on Blazor:

1.- Add Newtonsoft to your Blazor project (for Blazor client side wasm it is also possible because Newtonsoft is netstandard):

dotnet add package Newtonsoft.Json

2.- Referencing library, deserializing and iterating over deserialized objects:

@page "/counter"

@using Newtonsoft.Json

<table >
    <tr>
        @foreach (var c in dyn.cols)
        {
            <td style="border: 1px solid black;">@c.Value</td>    
        }
    </tr>
    @foreach (var r in dyn.rows)
    {
        <tr>
            @foreach (var d in r)
            {
                <td style="border: 1px solid black;">@d.Value</td>    
            }
        </tr>
    }
</table>

@code {
    dynamic dyn;
    string json_str = @" your json ";

    protected override void OnInitialized()
    {
          dyn = JsonConvert.DeserializeObject(json_str);
    }
}

Result:

Other info:

The whole json of the sample:

@code {
    dynamic dyn;
    string json_str = @"
        {
            ""cols"": [
                ""ID"",
                ""LastName"",
                ""Firstname"",
                ""middlename"",
                ""Suffix"",
                ""Title""

            ],
            ""rows"": [
                [
                    ""90"",
                    ""Dada"",
                    ""Mama"",
                    """",
                    """",
                    """"
                ],
                [
                    ""91"",
                    ""Dada1"",
                    ""Mama1"",
                    """",
                    """",
                    """"
                ],
            ]
        }
    ";



来源:https://stackoverflow.com/questions/57593280/how-to-deserialize-a-json-in-net-core-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!