如何在 .net core 3.0 中反序列化 json [英] How to deserialize a json in .net core 3.0
问题描述
我在使用 .Net Core 3.0 Blazor 应用程序反序列化 json 时遇到了一些麻烦.
我在 Visual Studio Preview 2019 中使用 .Net Core 3.0 Blazor 的框架
<代码>{列":[ID",姓",名",中间名字",后缀",标题"],行":[["90",达达",妈妈","","","]]}
预期结果:我想使用 blazor 动态加载这个 json 作为 Web 表单中的表格.我不介意为此使用任何 UI 框架,例如 devexpress 或 Telerik.
id LastNmae Firstname Middlename Suffix Title
90 爸爸妈妈
其他信息:
样本的整个json:
@code {动态动态;字符串 json_str = @"{"cols":[身份证","姓氏",名字","中间名",后缀","标题"],"行":[[90",达达",《妈妈》,"","",""],[91","达达1","妈妈1","","",""],]}";
已
免责声明:此时,您还可以使用System.Text.Json
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
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"",
"""",
"""",
""""
],
]
}
";
Edited:
Disclaimer: at this time, you can also use System.Text.Json
这篇关于如何在 .net core 3.0 中反序列化 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!