封装json格式数据
<div class="allPro">
<div class="line-big">
<div class="pro-item">
<div class="media-img">
<a href="/a/product/tiegao/2020/0529/33.html"><img src="/uploads/200529/1-200529110J4T1.jpg"></a>
</div>
<h3 class="text-center"><a href="/a/product/tiegao/2020/0529/33.html">颈肩腰腿痛冷敷贴</a></h3>
</div>
<div class="pro-item">
<div class="media-img">
<a href="/a/product/tiegao/2020/0529/39.html"><img src="/uploads/200529/1-20052911192W27.jpg"></a>
</div>
<h3 class="text-center"><a href="/a/product/tiegao/2020/0529/39.html">王氏秘贴 冷敷贴</a></h3>
</div>
</div>
</div>
js:
var tota = $(".proAll .pro-item").length;
var JsonData = {
a: "",
b: "",
c: ""
}
var aa = [];
for(var i = 0; i < tota; i++) {
var JsonData = new Object();
JsonData.a = $(".proImg").eq(i).attr("src");
JsonData.b = $(".proTitle").eq(i).html();
JsonData.c = $(".url").eq(i).attr("href");
aa.push(JsonData);
}
var json = JSON.stringify(aa);
console.log(json);
前端调用的时候,要转换:
var data = $.parseJSON(json);//