JavaScript – 将JSON数组转换为HTML元素

huangapple go评论74阅读模式
英文:

JavaScript - Convert JSON Array into HTML elements

问题

Here's the translated code part:

我有一个消息的 JSON 数组需要将其转换为 HTML 元素以在我的网页上显示我收到的 JSON 如下所示

{
    "data": [
        {
            "fieldData": {
                "Message": "Message 1",
                "Timestamp": "04/14/2023 11:39:13"
            }
        },
        {
            "fieldData": {
                "Message": "Message 2",
                "Timestamp": "04/13/2023 10:02:18"
            }
        },
        {
            "fieldData": {
                "Message": "Message 3",
                "Timestamp": "08/19/2021 19:51:21"
            }
        }
    ]
}

我需要从数组中提取每个 'Message'  'Timestamp' 并将它们放在类似以下的标签中用于第一个数组

<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>

因此使用上面的示例 JSON最终结果将是

<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>
<p class="from-them">Message 2</p>
<time class="timestamp-to">04/13/2023 10:02:18</time>
<p class="from-them">Message 3</p>
<time class="timestamp-to">08/19/2021 19:51:21</time>

这是我第一次尝试编写 JavaScript 函数将数组转换为单独的 HTML 元素不确定从何处开始

Please note that I've only provided the translation of the code part as requested. If you have any questions or need further assistance with this JavaScript task, please feel free to ask.

英文:

I have a JSON Array of messages that I need to convert into html elements to use for displaying in my web page. The JSON that I'm receiving looks like this:

	{
&quot;data&quot;: [
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 1&quot;,
&quot;Timestamp&quot;: &quot;04/14/2023 11:39:13&quot;
}
},
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 2&quot;,
&quot;Timestamp&quot;: &quot;04/13/2023 10:02:18&quot;
}
},
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 3&quot;,
&quot;Timestamp&quot;: &quot;08/19/2021 19:51:21&quot;
}
}
]
}

I need to extract each 'Message' and 'Timestamp' Value from the Array and place them in tags like the following for the first array:

&lt;p class=&quot;from-them&quot;&gt;Message 1&lt;/p&gt;
&lt;time class=&quot;timestamp-to&quot;&gt;04/14/2023 11:39:13&lt;/time&gt;

So using the sample JSON above the end result would be:

&lt;p class=&quot;from-them&quot;&gt;Message 1&lt;/p&gt;
&lt;time class=&quot;timestamp-to&quot;&gt;04/14/2023 11:39:13&lt;/time&gt;
&lt;p class=&quot;from-them&quot;&gt;Message 2&lt;/p&gt;
&lt;time class=&quot;timestamp-to&quot;&gt;04/13/2023 10:02:18&lt;/time&gt;
&lt;p class=&quot;from-them&quot;&gt;Message 3&lt;/p&gt;
&lt;time class=&quot;timestamp-to&quot;&gt;08/19/2021 19:51:21&lt;/time&gt;

It's my first time trying to write a JavaScript function to convert an array into individual html elements and not sure where to start here.

答案1

得分: 0

  1. 使用map()函数或简单地使用JSON.parse()将您的JSON转换为JavaScript可以使用的普通对象。
  2. 然后使用一个for循环遍历数组中的每个对象。
  3. 使用createElements来实际创建该元素。
let json_data = `{
  "data": [
    {
      "fieldData": {
        "Message": "Message 1",
        "Timestamp": "04/14/2023 11:39:13"
      }
    },
    {
      "fieldData": {
        "Message": "Message 2",
        "Timestamp": "04/13/2023 10:02:18"
      }
    },
    {
      "fieldData": {
        "Message": "Message 3",
        "Timestamp": "08/19/2021 19:51:21"
      }
    }
  ]
}`;

// 解析JSON
let data = JSON.parse(json_data);

// 循环创建元素
for (let messageIndex = 0; messageIndex < data.data.length; messageIndex++) {
  let message = data.data[messageIndex].fieldData.Message;
  let timestamp = data.data[messageIndex].fieldData.Timestamp;

  // 创建消息元素
  let messageElement = document.createElement('p');
  messageElement.classList.add('from-them');
  messageElement.textContent = message;
  document.body.appendChild(messageElement);

  // 创建时间戳元素
  let timestampElement = document.createElement('time');
  timestampElement.classList.add('timestamp-to');
  timestampElement.textContent = timestamp;
  document.body.appendChild(timestampElement);
}
英文:
  1. convert your JSON in a normal Object that JS can use either by using the map() function or by simply parsing the JSON with JSON.parse()
  2. then use a for-loop that loops through every object within the array
  3. Use createElements to actually create that element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let json_data = `{
&quot;data&quot;: [
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 1&quot;,
&quot;Timestamp&quot;: &quot;04/14/2023 11:39:13&quot;
}
},
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 2&quot;,
&quot;Timestamp&quot;: &quot;04/13/2023 10:02:18&quot;
}
},
{
&quot;fieldData&quot;: {
&quot;Message&quot;: &quot;Message 3&quot;,
&quot;Timestamp&quot;: &quot;08/19/2021 19:51:21&quot;
}
}
]
}`;
// parse JSON
let data = JSON.parse(json_data);
// loop to create the elements
for (let messageIndex = 0; messageIndex &lt; data.data.length; messageIndex++) {
let message = data.data[messageIndex].fieldData.Message;
let timestamp = data.data[messageIndex].fieldData.Timestamp;
// create Message
let messageElement = document.createElement(&#39;p&#39;);
messageElement.classList.add(&#39;from-them&#39;);
messageElement.textContent = message;
document.body.appendChild(messageElement);
// create Timestamp
let timestampElement = document.createElement(&#39;time&#39;);
timestampElement.classList.add(&#39;timestamp-to&#39;);
timestampElement.textContent = timestamp;
document.body.appendChild(timestampElement);
}

<!-- end snippet -->

答案2

得分: -1

创建一个HTML元素,需要做类似以下的操作:

var pElement = document.createElement('p'); //使用不同参数以创建不同元素
pElement.className = 'className';
pElement.textContent = 'textContent';
// 最后将其放入DOM中
document.body.appendChild(pElement);

现在,只需创建一个for循环(对于"data"),在每次迭代中创建p元素和time元素,然后将它们附加到DOM中。

英文:

To make a html element, you need to do something like:

var pElement = document.createElement(&#39;p&#39;); //use different parameter to make different elements
pElement.className = &#39;className&#39;;
pElement.textContent = &#39;textContent&#39;;
// and essentially you put it in the DOM
document.body.appendChild(pElement);

Now, you just need to make a for loop (for "data") and within each iteration make the p element and time element, and append them to the DOM.

huangapple
  • 本文由 发表于 2023年5月25日 13:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定