读取 json 文件数组中的数组

我正在构建一个将数据从 JSON 文件输出到表中的站点,但是我在获取要输出的内容时遇到了问题。这个 JSON 文件是从另一个显示文档的站点生成的,而我的站点只是创建一个表格以便于搜索这些文档。


2 个文档的示例 JSON:


    [{

        "title": "SampleTitleA",

        "lang": "en-US",

        "lastEdition": "2020-07-28",

        "version": "1.0",

        "metadata": [

        {

            "key": "sampleKeyA1",

            "label": "sampleLabelA1",

            "values": ["sampleValueA1"]

        },

        {

            "key": "sampleKeyA2",

            "label": "sampleLabelA2",

            "values": ["sampleValueA2"]

        }]

    },

    {

        "title": "SampleTitleB",

        "lang": "en-US",

        "lastEdition": "2020-07-28",

        "version": "1.0",

        "metadata": [

        {

            "key": "sampleKeyB1",

            "label": "sampleLabelB1",

            "values": ["sampleValueB1"]

        },

        {

            "key": "sampleKeyB2",

            "label": "sampleLabelB2",

            "values": ["sampleValueB2"]

        }]

    }]

我为此使用 DataTables ( https://datatables.net/examples/ajax/deep.html ) 并尝试按照它描述的去做。它并没有真正涵盖读取数组中的数组。

创建了一个表,但没有填充,并且在控制台中没有显示任何错误。有没有人为此目的使用数据表的经验?



慕虎7371278
浏览 90回答 2
2回答

慕标琳琳

检查这是否可以帮助您。var data = {    "title": "SampleTitle",    "lang": "en-US",    "lastEdition": "2020-07-28",    "version": "1.0",    "metadata": [    {        "key": "sampleKey1",        "label": "sampleLabel1",        "values": ["sampleValue1"]    },    {        "key": "sampleKey2",        "label": "sampleLabel2",        "values": ["sampleValue2"]    }]}var result = { data: data.metadata[1].values[0], "defaultContent": "-" }console.log(result);

慕田峪7331174

您的 JSON 数据结构是一个数组——所有内容都包含在一个中[...]——因此 DataTables 可以遍历该数组以生成其表行。data这是一个示例,其中除了列定义(和列标题)外,所有内容都从您的代码中删除:<script type="text/javascript">&nbsp;$(document).ready(function() {&nbsp; &nbsp; $('#example').DataTable({&nbsp; &nbsp; &nbsp; &nbsp; ajax: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // my test URL:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'http://localhost:7000/sample2',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataSrc: ''&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "columns": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: 'Title', data: 'title' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: 'Language', data: 'lang' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: 'Key', data: 'metadata[0].key' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: 'Label', data: 'metadata[0].label' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: 'First Value', data: 'metadata[0].values[0]' }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; } );&nbsp; } );</script>这会生成一个如下所示的表:这是如何运作的?默认情况下,DataTables 期望 JSON 结构为以下之一:包含其他对象数组的对象:{&nbsp;"data":&nbsp;[&nbsp;{...},{...},...&nbsp;]&nbsp;}包含数组数组的对象:{&nbsp;"data":&nbsp;[&nbsp;[...],[...],...&nbsp;]&nbsp;}在这两种情况下,数组都有一个名称(在本例中为data)。在您的情况下,如前所述,您的数据只是一个普通的对象数组:[&nbsp;{...},&nbsp;{...},...&nbsp;]因为数组没有名称,所以我们需要dataSrc: ''在 DataTable 定义中使用它来表示缺少名称。之后,您可以引用需要显示的值,例如data: 'title'.对于该metadata部分,它本身就是一个引用对象数组的标签:"metadata":&nbsp;[&nbsp;{...}&nbsp;]但是,在这种情况下,数组只包含一个对象。我们可以使用[0]- 引用元数据数组中的第一个对象,然后我们可以访问该对象中的值 - 例如,通过使用:data: 'metadata[0].label'。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript