Convert HTML list to JSON

advertisements

I have a little problem with my HTML template for convert to JSON. I wish convert a list of HTML messages to JSON. Thank you in advance for your response.

HTML Template

<div class="message">
    <div class="message-data">
        <div class="infos">
            <h4 class="pseudo">Name</h4>
            <span class="date">MM/DD/YYYY</span>
            <span class="time">HH:MM</span>
        </div>
    </div>
    <div class="message-content">
        <div class="message-text">
            Message here
        </div>
    </div>
</div>

<!-- Others .message -->

JSON Render

{
    "list": [
        {
            "pseudo": "Name",
            "date": "MM/DD/YYYY",
            "time": "HH:MM",
            "message": "Message here"
        },
        /* ... */
    ]
}


even if it doesn't look that cool I think this will be much faster:

var pseudo = document.getElementsByClassName("pseudo");
var date = document.getElementsByClassName("date");
var time = document.getElementsByClassName("time");
var message = document.getElementsByClassName("message-text");

var list = [];
for(var i=0, l=pseudo.length;i<l;i++) {
 list.push({
    pseudo: pseudo[i].textContent,
    data: date[i].textContent,
    time: time[i].textContent,
    message: message[i].textContent
 })
}
console.log(list);

.. depending on your file structure this may not be the best solution