本文介绍了如何在.net Core 3.0中反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.Net Core 3.0 Blazor应用程序反序列化json时,我遇到了一些麻烦.

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

我正在将.Net Core 3.0 Blazor与Visual Studio Preview 2019一起使用

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",
            "",
            "",
            ""

        ]
    ]
}

预期结果:我想使用blazor将这个json动态加载为Web表单中的表格.我不介意为此使用任何UI框架,例如devexpress或telerik.

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名中间名后缀标题

id LastNmae Firstname middlename Suffix Title

90达达妈妈

预期结果

推荐答案

在Blazor上完成的步骤:

1.-将Newtonsoft添加到您的Blazor项目中(对于Blazor客户端wasm,也可能是因为Newtonsoft是netstandard):

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.-引用库,对反序列化的对象进行反序列化和迭代:

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);
    }
}

结果:

示例的整个json:

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

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

这篇关于如何在.net Core 3.0中反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:13