简体   繁体   中英

Javascript Get data from HTML elements and save them in json

I cannot figure out how to sort through this heap of data, can you please point me in the right direction?

Okay so the data I need to sort through looks like this..

<tr>
    <td><a href="/wiki/Assault/Gallery#Collection" title="Assault/Gallery">Assault</a>
    </td>
    <td><a href="/wiki/SG_553" title="SG 553">SG 553</a>
    </td>
    <td><span class="common">Tornado</span>
    </td>
    <td data-sort-value="0"><span class="common">Consumer</span>
    </td>
    <td><a rel="nofollow" class="external text exitstitial" href="http://steamcommunity.com/market/search?appid=730&amp;q=SG+553+Tornado">view</a>
    </td>
    <td>
    </td>
    <td>&nbsp;√
    </td>
</tr>

It's CS:GO weapon data I can't seem to find elsewhere. The data I need to get and save is the Name: Tornado, weapon: SG 553, collection: Assault and value: 0 (the data-sort-vale)

How do I convert it into json?

You'll want to use jQuery. As long as the table is consistently formatted, I would do something like this:

var data = [];

$('table tr').each(function () {
   data.push({
      name: $(this).children('td').eq(2).text(),
      weapon: $(this).children('td').eq(1).text(),
      collection: $(this).children('td').eq(0).text(),
      value: $(this).children('td').eq(3).data('sort-value')
   })
})

Where .eq( # ) is the index of the table cell in the row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM