I would like to save some of HMTL data to JSON file and need help with code which generates JSON using jQuery. HTML structure looks like this, but there will be many departments with similar structure including same tags and classes :
<div class="department_one">
<h1>Name Of Manufacturer</h1>
<div class="project">
<div class="project_name">Name of Projects</div>
<div class="chart">
<span class="bar red"></span>
<span class="bar red"></span>
<span class="bar red"></span>
<span class="bar red"></span>
<span class="bar red"></span>
<span class="bar red"></span>
<span class="bar blue"></span>
<span class="bar green"></span>
<span class="bar green"></span>
<span class="bar green"></span>
<span class="bar gray"></span>
</div>
</div>
</div>
</div>
The data to generated to JSON file:
- name of manufacturer (from
<h1></h1>
); - name of project (from
<div class="project_name">
); - span class and sub class attributes (they all have same main class bar, but sub-classes are different and sub-classes may repeated, as you see there are two span tags with sub-class .red)
I need most advise to how structure JSON if every time there will be different amounts of same sub classes of span tags.
the expected JSON:
{
"holder": [
{
"deptName": "main department",
"project": [
{
"projName": "Proj_1.2",
"chartItems": [
{
"color": "grey",
"amount": 3
},
{
"color": "red",
"amount": 7
},
{
"color": "blue",
"amount": 2
},
{
"color": "green",
"amount": 1
}
]
}
]
},
{
"deptName": "other department",
"project": [
{
"projName": "Proj_2.2",
"chartItems": [
{
"color": "grey",
"amount": 1
},
{
"color": "red",
"amount": 1
},
{
"color": "blue",
"amount": 3
},
{
"color": "green",
"amount": 5
}
]
}
]
}
]
}