在变量中有一个Json数据,并希望在asp.net中以表格形式显示该数据

时间:2019-12-24 02:50:34

标签: c# asp.net arrays json tabular

我想使用ASP.NET将其转换为表格格式

responsebody= [{
        "name": "apple.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.com",
        "status": "available",
        "classkey": "domcno"
    },
    {
        "name": "microsoft.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "apple.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "microsoft.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.org",
        "status": "unknown",
        "classkey": "domcno"
    }]

3 个答案:

答案 0 :(得分:1)

我猜答案取决于您使用的是ASP.NET MVC还是WebForms,内置类型还是第3方库。

以下是MVC的示例。

1。您的控制器操作类似于:

public ActionResult DisplayJsonData()
        {
            var json = @"{
                'items' : [{
                        'name': 'apple.com',
                        'status': 'regthroughothers',
                        'classkey': 'domcno'
                    },
                    {
                        'name': 'asdfgqwx.com',
                        'status': 'available',
                        'classkey': 'domcno'
                    },
                    {
                        'name': 'microsoft.org',
                        'status': 'unknown',
                        'classkey': ''
                    },
                    {
                        'name': 'apple.org',
                        'status': 'unknown',
                        'classkey': ''
                    },
                    {
                        'name': 'microsoft.com',
                        'status': 'regthroughothers',
                        'classkey': 'domcno'
                    },
                    {
                        'name': 'asdfgqwx.org',
                        'status': 'unknown',
                        'classkey': 'domcno'
                    }
                ]
            }"; 

            var model = JsonConvert.DeserializeObject<DisplayJsonModel>(json);
            return View(model);
        }

和DisplayJsonData.cshtml视图:

@model WebApplication2.Models.DisplayJsonModel
@{
    ViewBag.Title = "DisplayJsonData";
}

<h2>DisplayJsonData</h2>

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Status</th>
            <th>Class Key</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Items)
        {
            <tr>
                <td>@item.name</td>
                <td>@item.status</td>
                <td>@item.classkey</td>
            </tr>
        }
    </tbody>
</table>
  1. 如上所述,您还需要解析JSON。

    公共类DisplayJsonModel     {         公共JsonItem []项{get;组; }     }

    public class JsonItem
    {
        public string name { get; set; }
        public string status { get; set; }
        public string classkey { get; set; }
    }
    

这是纯JS:

<table class="table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Status</th>
        <th>Class Key</th>
    </tr>
    </thead>
    <tbody id="jsTableBody"></tbody>
</table>

<script>
    var responsebody = [
        {
            "name": "apple.com",
            "status": "regthroughothers",
            "classkey": "domcno"
        },
        {
            "name": "asdfgqwx.com",
            "status": "available",
            "classkey": "domcno"
        },
        {
            "name": "microsoft.org",
            "status": "unknown",
            "classkey": ""
        },
        {
            "name": "apple.org",
            "status": "unknown",
            "classkey": ""
        },
        {
            "name": "microsoft.com",
            "status": "regthroughothers",
            "classkey": "domcno"
        },
        {
            "name": "asdfgqwx.org",
            "status": "unknown",
            "classkey": "domcno"
        }
    ];

    var tableRows = [];
    for (var i = 0; i < responsebody.length; i++) {
        tableRows.push("<tr>" +
            "<td>" + responsebody[i].name + "</td>" + 
            "<td>" + responsebody[i].status + "</td>" + 
            "<td>" + responsebody[i].classkey + "</td>" + 
            "</tr>");
    }
    var tableBody = document.getElementById("jsTableBody");
    tableBody.innerHTML = tableRows.join('');
</script>

对于Web表单 您可能想研究在aspx页面中使用asp:DataGrid之类的东西:

<asp:DataGrid runat="server" ID="grid" AutoGenerateColumns="False" CssClass="table table-bordered">
<Columns>
  <asp:BoundColumn DataField="name" HeaderText="Name" />
  <asp:BoundColumn DataField="status" HeaderText="Status" />
  <asp:BoundColumn DataField="classkey" HeaderText="Class Key" />
</Columns>
</asp:DataGrid>

并在代码背后:

var model = JsonConvert.DeserializeObject<DisplayJsonModel>(json);
grid.DataSource = model.Items;
grid.DataBind();

答案 1 :(得分:0)

您将需要Newtonsoft.JsonConvert NuGet包将JSON转换为C#兼容类型。在解决方案资源管理器中右键单击您的项目,然后单击“管理NuGet软件包”,然后搜索并安装它。

您还需要创建一个新类,以正确的格式存储数据。右键单击您的项目,将鼠标悬停在“添加”上,然后单击“类”。假设您所有的数据都是字符串格式:

public class Data
    {
        public string name { get; set; }
        public string status { get; set; }
        public string classkey { get; set; }
    }

返回JSON变量所在的位置,添加using Newtonsoft.Json;,然后使用如下代码:

List<Data> deserializedData = JsonConvert.DeserializeObject<List<Data>>(your variable name here);

这会将您的数据保存在“数据类型”列表中。从那里,您可以将其插入SQLite数据库或执行任何操作,但是您需要使您的问题更明确地涉及“表格”的含义。

答案 2 :(得分:0)

Visual Studio

第1步:通过JSON或(clipboard + right-click)将CtrC复制到C

[{
        "name": "apple.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.com",
        "status": "available",
        "classkey": "domcno"
    },
    {
        "name": "microsoft.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "apple.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "microsoft.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.org",
        "status": "unknown",
        "classkey": "domcno"
    }]

第2步:将指针放到要编写的位置。

选择Edit >> Paste Special >> Paste JSON as Classes

  

结果:

    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string name { get; set; }
        public string status { get; set; }
        public string classkey { get; set; }
    }

Step3:您可以根据需要更改某些内容。然后,使用JsonConvert.DeserializeObject<Foo>(yourString)进行工作。